11use gpui:: {
2- AppContext , Application , Bounds , Focusable , TitlebarOptions , WindowBounds , WindowDecorations ,
3- WindowKind , WindowOptions , point, px, size,
2+ App , AppContext , Application , BorrowAppContext , Bounds , Entity , Focusable , TitlebarOptions ,
3+ WindowBounds , WindowDecorations , WindowHandle , WindowKind , WindowOptions , point, px, size,
44} ;
55
66use crate :: config:: Config ;
77use crate :: entries:: SearchEntries ;
88use crate :: ipc:: client:: { SocketClient , SocketMessage } ;
99use crate :: ipc:: server:: SocketServer ;
10- use crate :: ui:: { CloseWaystart , Waystart } ;
10+ use crate :: ui:: Waystart ;
1111
1212mod cli;
1313mod config;
@@ -16,79 +16,85 @@ mod ipc;
1616mod ui;
1717
1818fn main ( ) {
19- match cli:: Waystart :: from_env_or_exit ( ) . subcommand {
20- cli:: WaystartCmd :: Standalone ( _) => match SocketClient :: try_connect ( ) . ok ( ) {
21- Some ( client) => client . send_message_socket ( SocketMessage :: Show ) ,
22- None => {
23- start_app ( false ) ;
19+ let daemon = match cli:: Waystart :: from_env_or_exit ( ) . subcommand {
20+ cli:: WaystartCmd :: Standalone ( _) => {
21+ if let Ok ( client) = SocketClient :: try_connect ( ) {
22+ client . send_message_socket ( SocketMessage :: Open ) ;
23+ return ;
2424 }
25- } ,
26-
25+ // Start without daemon
26+ false
27+ }
2728 cli:: WaystartCmd :: Daemon ( daemon) => {
2829 if daemon. exit {
2930 let client = SocketClient :: connect ( ) ;
3031 client. send_message_socket ( SocketMessage :: Quit ) ;
31- } else {
32- start_app ( true ) ;
32+ return ;
3333 }
34+ // Start with daemon
35+ true
3436 }
35-
36- cli:: WaystartCmd :: Show ( _) => {
37+ cli:: WaystartCmd :: Open ( _) => {
3738 let client = SocketClient :: connect ( ) ;
38- client. send_message_socket ( SocketMessage :: Show ) ;
39+ client. send_message_socket ( SocketMessage :: Open ) ;
40+ return ;
3941 }
40-
41- cli:: WaystartCmd :: Hide ( _) => {
42+ cli:: WaystartCmd :: Close ( _) => {
4243 let client = SocketClient :: connect ( ) ;
43- client. send_message_socket ( SocketMessage :: Hide ) ;
44+ client. send_message_socket ( SocketMessage :: Close ) ;
45+ return ;
4446 }
45- }
46- }
47+ cli:: WaystartCmd :: Toggle ( _) => {
48+ let client = SocketClient :: connect ( ) ;
49+ client. send_message_socket ( SocketMessage :: Toggle ) ;
50+ return ;
51+ }
52+ } ;
4753
48- fn start_app ( daemonize : bool ) {
4954 Application :: new ( )
5055 . with_assets ( ui:: Assets )
51- . keep_running ( daemonize )
56+ . keep_running ( daemon )
5257 . run ( move |cx| {
5358 ui:: init ( cx) ;
5459 cx. set_global ( SearchEntries :: load ( ) ) ;
5560 cx. set_global ( Config :: load ( ) ) ;
56- cx. on_action ( move |_: & CloseWaystart , cx| {
57- if daemonize {
58- cx. hide ( ) ;
59- } else {
60- cx. quit ( ) ;
61- }
62- } ) ;
6361
64- let bounds = Bounds :: centered ( None , size ( px ( 800. ) , px ( 500. ) ) , cx) ;
65- let window = cx
66- . open_window (
67- WindowOptions {
68- kind : WindowKind :: PopUp ,
69- is_movable : true ,
70- show : !daemonize,
71- focus : !daemonize,
72- window_bounds : Some ( WindowBounds :: Windowed ( bounds) ) ,
73- window_decorations : Some ( WindowDecorations :: Client ) ,
74- titlebar : Some ( TitlebarOptions {
75- title : Some ( "Waystart" . into ( ) ) ,
76- appears_transparent : true ,
77- traffic_light_position : Some ( point ( px ( -100.0 ) , px ( 0.0 ) ) ) ,
78- } ) ,
79- ..Default :: default ( )
80- } ,
81- |window, cx| {
82- let root = cx. new ( Waystart :: new) ;
83- window. focus ( & root. focus_handle ( cx) ) ;
84- root
85- } ,
86- )
87- . unwrap ( ) ;
62+ let waystart = cx. new ( Waystart :: new) ;
8863
89- if daemonize {
90- let server = SocketServer :: new ( cx. to_async ( ) , window ) ;
64+ if daemon {
65+ let server = SocketServer :: new ( cx. to_async ( ) , waystart ) ;
9166 server. listen ( ) ;
67+ } else {
68+ open_window ( cx, waystart) ;
9269 }
9370 } ) ;
9471}
72+
73+ pub fn open_window ( cx : & mut App , waystart : Entity < Waystart > ) -> WindowHandle < Waystart > {
74+ cx. update_global ( |entries : & mut SearchEntries , _| entries. sort_by_frequency ( ) ) ;
75+ cx. update_entity ( & waystart, |waystart : & mut Waystart , cx| {
76+ waystart. reset_search ( cx)
77+ } ) ;
78+
79+ let bounds = Bounds :: centered ( None , size ( px ( 800. ) , px ( 500. ) ) , cx) ;
80+ cx. open_window (
81+ WindowOptions {
82+ kind : WindowKind :: PopUp ,
83+ is_resizable : false ,
84+ is_minimizable : false ,
85+ window_bounds : Some ( WindowBounds :: Windowed ( bounds) ) ,
86+ window_decorations : Some ( WindowDecorations :: Client ) ,
87+ titlebar : Some ( TitlebarOptions {
88+ title : Some ( "Waystart" . into ( ) ) ,
89+ appears_transparent : true ,
90+ traffic_light_position : Some ( point ( px ( -100.0 ) , px ( 0.0 ) ) ) ,
91+ } ) ,
92+ ..Default :: default ( )
93+ } ,
94+ |window, cx| {
95+ window. focus ( & waystart. focus_handle ( cx) ) ;
96+ waystart
97+ } ,
98+ )
99+ . unwrap ( )
100+ }
0 commit comments