11use crate :: dispatcher:: Dispatcher ;
22use crate :: messages:: prelude:: * ;
33pub use graphene_std:: uuid:: * ;
4+ use std:: sync:: OnceLock ;
45
5- // TODO: serialize with serde to save the current editor state
66pub struct Editor {
77 pub dispatcher : Dispatcher ,
88}
99
1010impl Editor {
11- /// Construct the editor.
12- /// Remember to provide a random seed with `editor::set_uuid_seed(seed)` before any editors can be used.
13- pub fn new ( ) -> Self {
11+ pub fn new ( environment : Environment , uuid_random_seed : u64 ) -> Self {
12+ ENVIRONMENT . set ( environment) . expect ( "Editor shoud only be initialized once" ) ;
13+ graphene_std:: uuid:: set_uuid_seed ( uuid_random_seed) ;
14+
1415 Self { dispatcher : Dispatcher :: new ( ) }
1516 }
1617
1718 #[ cfg( test) ]
1819 pub ( crate ) fn new_local_executor ( ) -> ( Self , crate :: node_graph_executor:: NodeRuntime ) {
20+ let _ = ENVIRONMENT . set ( * Editor :: environment ( ) ) ;
21+ graphene_std:: uuid:: set_uuid_seed ( 0 ) ;
22+
1923 let ( runtime, executor) = crate :: node_graph_executor:: NodeGraphExecutor :: new_with_local_runtime ( ) ;
20- let dispatcher = Dispatcher :: with_executor ( executor) ;
21- ( Self { dispatcher } , runtime)
24+ let editor = Self {
25+ dispatcher : Dispatcher :: with_executor ( executor) ,
26+ } ;
27+
28+ ( editor, runtime)
2229 }
2330
2431 pub fn handle_message < T : Into < Message > > ( & mut self , message : T ) -> Vec < FrontendMessage > {
@@ -32,9 +39,53 @@ impl Editor {
3239 }
3340}
3441
35- impl Default for Editor {
36- fn default ( ) -> Self {
37- Self :: new ( )
42+ static ENVIRONMENT : OnceLock < Environment > = OnceLock :: new ( ) ;
43+ impl Editor {
44+ #[ cfg( not( test) ) ]
45+ pub fn environment ( ) -> & ' static Environment {
46+ ENVIRONMENT . get ( ) . expect ( "Editor environment accessed before initialization" )
47+ }
48+
49+ #[ cfg( test) ]
50+ pub fn environment ( ) -> & ' static Environment {
51+ & Environment {
52+ platform : Platform :: Desktop ,
53+ host : Host :: Linux ,
54+ }
55+ }
56+ }
57+
58+ #[ derive( Clone , Copy , Debug ) ]
59+ pub struct Environment {
60+ pub platform : Platform ,
61+ pub host : Host ,
62+ }
63+ #[ derive( Clone , Copy , Debug ) ]
64+ pub enum Platform {
65+ Desktop ,
66+ Web ,
67+ }
68+ #[ derive( Clone , Copy , Debug ) ]
69+ pub enum Host {
70+ Windows ,
71+ Mac ,
72+ Linux ,
73+ }
74+ impl Environment {
75+ pub fn is_desktop ( & self ) -> bool {
76+ matches ! ( self . platform, Platform :: Desktop )
77+ }
78+ pub fn is_web ( & self ) -> bool {
79+ matches ! ( self . platform, Platform :: Web )
80+ }
81+ pub fn is_windows ( & self ) -> bool {
82+ matches ! ( self . host, Host :: Windows )
83+ }
84+ pub fn is_mac ( & self ) -> bool {
85+ matches ! ( self . host, Host :: Mac )
86+ }
87+ pub fn is_linux ( & self ) -> bool {
88+ matches ! ( self . host, Host :: Linux )
3889 }
3990}
4091
0 commit comments