mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-27 12:38:12 +08:00
Replace globals with editor environment (#3656)
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
@@ -1,24 +1,31 @@
|
||||
use crate::dispatcher::Dispatcher;
|
||||
use crate::messages::prelude::*;
|
||||
pub use graphene_std::uuid::*;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
// TODO: serialize with serde to save the current editor state
|
||||
pub struct Editor {
|
||||
pub dispatcher: Dispatcher,
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
/// Construct the editor.
|
||||
/// Remember to provide a random seed with `editor::set_uuid_seed(seed)` before any editors can be used.
|
||||
pub fn new() -> Self {
|
||||
pub fn new(environment: Environment, uuid_random_seed: u64) -> Self {
|
||||
ENVIRONMENT.set(environment).expect("Editor shoud only be initialized once");
|
||||
graphene_std::uuid::set_uuid_seed(uuid_random_seed);
|
||||
|
||||
Self { dispatcher: Dispatcher::new() }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn new_local_executor() -> (Self, crate::node_graph_executor::NodeRuntime) {
|
||||
let _ = ENVIRONMENT.set(*Editor::environment());
|
||||
graphene_std::uuid::set_uuid_seed(0);
|
||||
|
||||
let (runtime, executor) = crate::node_graph_executor::NodeGraphExecutor::new_with_local_runtime();
|
||||
let dispatcher = Dispatcher::with_executor(executor);
|
||||
(Self { dispatcher }, runtime)
|
||||
let editor = Self {
|
||||
dispatcher: Dispatcher::with_executor(executor),
|
||||
};
|
||||
|
||||
(editor, runtime)
|
||||
}
|
||||
|
||||
pub fn handle_message<T: Into<Message>>(&mut self, message: T) -> Vec<FrontendMessage> {
|
||||
@@ -32,9 +39,53 @@ impl Editor {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Editor {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
static ENVIRONMENT: OnceLock<Environment> = OnceLock::new();
|
||||
impl Editor {
|
||||
#[cfg(not(test))]
|
||||
pub fn environment() -> &'static Environment {
|
||||
ENVIRONMENT.get().expect("Editor environment accessed before initialization")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn environment() -> &'static Environment {
|
||||
&Environment {
|
||||
platform: Platform::Desktop,
|
||||
host: Host::Linux,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Environment {
|
||||
pub platform: Platform,
|
||||
pub host: Host,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Platform {
|
||||
Desktop,
|
||||
Web,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Host {
|
||||
Windows,
|
||||
Mac,
|
||||
Linux,
|
||||
}
|
||||
impl Environment {
|
||||
pub fn is_desktop(&self) -> bool {
|
||||
matches!(self.platform, Platform::Desktop)
|
||||
}
|
||||
pub fn is_web(&self) -> bool {
|
||||
matches!(self.platform, Platform::Web)
|
||||
}
|
||||
pub fn is_windows(&self) -> bool {
|
||||
matches!(self.host, Host::Windows)
|
||||
}
|
||||
pub fn is_mac(&self) -> bool {
|
||||
matches!(self.host, Host::Mac)
|
||||
}
|
||||
pub fn is_linux(&self) -> bool {
|
||||
matches!(self.host, Host::Linux)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user