1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use crate::{
    makepad_code_editor::code_editor::*,
    makepad_platform::*,
    makepad_draw::*,
    makepad_widgets::*,
    makepad_widgets::file_tree::*,
    file_system::file_system::*,
    build_manager::{
        run_view::*,
        log_list::{
            LogListAction
        },
        run_list::{
            RunListAction
        },
        build_manager::{
            BuildManager,
            BuildManagerAction
        },
    }
};

live_design!{
    import makepad_draw::shader::std::*;
    import makepad_widgets::base::*;
    import makepad_widgets::theme_desktop_dark::*;
    import makepad_code_editor::code_editor::CodeEditor;
    
    import makepad_studio::build_manager::run_view::RunView;
    import makepad_studio::build_manager::log_list::LogList;
    import makepad_studio::build_manager::run_list::RunList;
    
    Logo = <Button> {
        draw_icon: {
            svg_file: dep("crate://self/resources/logo_makepad.svg"),
            fn get_color(self) -> vec4 {
                return #xffffff
            }
        }
        icon_walk: {width: 300.0, height: Fit}
        draw_bg: {
            fn pixel(self) -> vec4 {
                let sdf = Sdf2d::viewport(self.pos * self.rect_size);
                return sdf.result
            }
        }
        margin: {top: 20.0, right: 0.0, bottom: 30.0, left: 0.0}
        padding: 0.0
        text: ""
    }
    
    App = {{App}} {
        ui: <Window> {
            caption_bar = {visible: true, caption_label = {label = {text: "Makepad Studio"}}},
            window: {inner_size: vec2(1600, 900)},
            window_menu = {
                main = Main {items: [app, file, edit, selection, view, run, window, help]}
                
                app = Sub {name: "Makepad Studio", items: [about, line, settings, line, quit]}
                about = Item {name: "About Makepad Studio", enabled: false}
                settings = Item {name: "Settings", enabled: false}
                quit = Item {name: "Quit Makepad Studio", key: KeyQ}
                
                file = Sub {name: "File", items: [new_file, new_window, line, save_as, line, rename, line, close_editor, close_window]}
                new_file = Item {name: "New File", enabled: false, shift: true, key: KeyN}
                new_window = Item {name: "New Window", enabled: false, shift: true, key: KeyN}
                save_as = Item {name: "Save As", enabled: false}
                rename = Item {name: "Rename", enabled: false}
                close_editor = Item {name: "Close Editor", enabled: false}
                close_window = Item {name: "Close Window", enabled: false}
                
                edit = Sub {name: "Edit", items: [undo, redo, line, cut, copy, paste, line, find, replace, line, find_in_files, replace_in_files]}
                undo = Item {name: "Undo", enabled: false}
                redo = Item {name: "Redo", enabled: false}
                cut = Item {name: "Cut", enabled: false}
                copy = Item {name: "Copy", enabled: false}
                paste = Item {name: "Paste", enabled: false}
                find = Item {name: "Find", enabled: false}
                replace = Item {name: "Replace", enabled: false}
                find_in_files = Item {name: "Find in Files", enabled: false}
                replace_in_files = Item {name: "Replace in Files", enabled: false}
                
                selection = Sub {name: "Selection", items: [select_all]}
                select_all = Item {name: "Select All", enabled: false}
                
                view = Sub {name: "View", items: [select_all]}
                zoom_in = Item {name: "Zoom In", enabled: false}
                zoom_out = Item {name: "Zoom Out", enabled: false}
                select_all = Item {name: "Enter Full Screen", enabled: false}
                
                run = Sub {name: "Run", items: [run_program]}
                run_program = Item {name: "Run Program", enabled: false}
                
                window = Sub {name: "Window", items: [minimize, zoom, line, all_to_front]}
                minimize = Item {name: "Minimize", enabled: false}
                zoom = Item {name: "Zoom", enabled: false}
                all_to_front = Item {name: "Bring All to Front", enabled: false}
                
                help = Sub {name: "Help", items: [about]}
                
                line = Line,
            }
            body = {dock = <Dock> {
                height: Fill,
                width: Fill
                
                root = Splitter {
                    axis: Horizontal,
                    align: FromA(230.0),
                    a: file_tree_tabs,
                    b: split1
                }
                
                split1 = Splitter {
                    axis: Vertical,
                    align: FromB(200.0),
                    a: split2,
                    b: log_tabs
                }
                
                split2 = Splitter {
                    axis: Horizontal,
                    align: Weighted(0.5),
                    a: edit_tabs,
                    b: run_tabs
                }
                
                
                
                file_tree_tabs = Tabs {
                    tabs: [file_tree, search, run_list],
                    selected: 2
                }
                
                edit_tabs = Tabs {
                    tabs: [edit_first],
                    selected: 0
                }
                
                log_tabs = Tabs {
                    tabs: [log_list],
                    selected: 0
                }
                
                run_tabs = Tabs {
                    tabs: [run_first],
                    selected: 0
                }
                
                file_tree = Tab {
                    name: "Explore",
                    closable: false,
                    kind: FileTree
                }
                
                search = Tab {
                    name: "Search"
                    closable: false,
                    kind: Search
                }
                
                run_first = Tab {
                    name: "View"
                    closable: false,
                    kind: RunFirst
                }
                edit_first = Tab {
                    name: "Edit"
                    closable: false,
                    kind: EditFirst
                }
                
                run_list = Tab {
                    name: "Run"
                    closable: false,
                    kind: RunList
                }
                
                file1 = Tab {
                    name: "app.rs",
                    closable: true,
                    kind: CodeEditor
                }
                
                log_list = Tab {
                    name: "Log",
                    closable: false,
                    kind: LogList
                }
                
                CodeEditor = <CodeEditor> {}
                EditFirst = <RectView> {
                    draw_bg: {color: #052329}
                    <View> {
                        width: Fill,
                        height: Fill
                        align: {
                            x: 0.5,
                            y: 0.5
                        }
                        flow: Down
                        
                            <Logo> {}
                        
                        <Label> {
                            text: "Welcome to\nMakepad \n\n欢迎来到\nMakepad"
                            width: Fit,
                            margin: {left: 200}
                            draw_text: {
                                text_style: {
                                    font_size: 20.0,
                                    height_factor: 1.0,
                                    font: {path: dep("crate://makepad-widgets/resources/GoNotoKurrent-Regular.ttf")}
                                },
                            }
                        }
                    }
                    
                }
                RunFirst = <RectView> {
                    draw_bg: {color: #4}
                    <View> {
                        width: Fill,
                        height: Fill
                        align: {
                            x: 0.5,
                            y: 0.5
                        }
                        flow: Down
                            <Logo> {
                            draw_icon: {
                                fn get_color(self) -> vec4 {
                                    return #7
                                }
                            }
                        }
                    }
                    
                }
                RunList = <RunList> {
                }
                Search = <RectView> {
                    draw_bg: {color: #2}
                }
                RunView = <RunView> {}
                FileTree = <FileTree> {}
                LogList = <LogList> {}
            }}
        }
    }
}

#[derive(Live)]
pub struct App {
    #[live] ui: WidgetRef,
    #[live] build_manager: BuildManager,
    #[rust] file_system: FileSystem,
}

impl LiveHook for App {
    fn before_live_design(cx: &mut Cx) {
        crate::makepad_widgets::live_design(cx);
        crate::makepad_code_editor::live_design(cx);
        crate::build_manager::build_manager::live_design(cx);
        crate::build_manager::run_list::live_design(cx);
        crate::build_manager::log_list::live_design(cx);
        crate::build_manager::run_view::live_design(cx);
        // for macos
        cx.start_stdin_service();
    }
    
    fn after_new_from_doc(&mut self, cx: &mut Cx) {
        self.file_system.init(cx);
        self.build_manager.init(cx);
        
        //self.file_system.request_open_file(live_id!(file1), "examples/news_feed/src/app.rs".into());
    }
}

app_main!(App);

impl App {
    fn open_code_file_by_path(&mut self, cx: &mut Cx, path: &str) {
        let tab_id = LiveId::unique();
        if let Some(file_id) = self.file_system.path_to_file_node_id(&path) {
            self.file_system.request_open_file(tab_id, file_id);
            let dock = self.ui.dock(id!(dock));
            dock.create_and_select_tab(cx, live_id!(edit_tabs), tab_id, live_id!(CodeEditor), "".to_string(), TabClosable::Yes);
            self.file_system.ensure_unique_tab_names(cx, &dock)
        }
    }
}

impl AppMain for App {
    
    
    fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
        let dock = self.ui.dock(id!(dock));
        let file_tree = self.ui.file_tree(id!(file_tree));
        let log_list = self.ui.portal_list(id!(log_list));
        let run_list = self.ui.flat_list(id!(run_list));
        if let Event::Draw(event) = event {
            //let dt = profile_start();
            let cx = &mut Cx2d::new(cx, event);
            while let Some(next) = self.ui.draw_widget(cx).hook_widget() {
                
                if let Some(mut file_tree) = file_tree.has_widget(&next).borrow_mut() {
                    file_tree.set_folder_is_open(cx, live_id!(root).into(), true, Animate::No);
                    self.file_system.draw_file_node(
                        cx,
                        live_id!(root).into(),
                        &mut *file_tree
                    );
                }
                else if let Some(mut run_view) = next.as_run_view().borrow_mut() {
                    let current_id = dock.drawing_item_id().unwrap();
                    run_view.draw(cx, current_id, &mut self.build_manager);
                }
                else if let Some(mut log_list) = log_list.has_widget(&next).borrow_mut() {
                    self.build_manager.draw_log(cx, &mut *log_list);
                }
                else if let Some(mut run_list) = run_list.has_widget(&next).borrow_mut() {
                    self.build_manager.draw_run_list(cx, &mut *run_list);
                }
                else if let Some(mut code_editor) = next.as_code_editor().borrow_mut() {
                    // lets fetch a session
                    let current_id = dock.drawing_item_id().unwrap();
                    if let Some(session) = self.file_system.get_session_mut(current_id) {
                        code_editor.draw(cx, session);
                    }
                }
            }
            //profile_end!(dt);
            return
        }
        
        if let Event::Destruct = event {
            self.build_manager.clear_active_builds();
        }
        
        if let Event::KeyDown(KeyEvent {
            key_code,
            modifiers: KeyModifiers {logo, control, ..},
            ..
        }) = event {
            if *control || *logo {
                if let KeyCode::Backtick = key_code {
                    self.build_manager.start_recompile(cx);
                }
                else if let KeyCode::KeyK = key_code {
                    self.build_manager.clear_log(cx, &dock, &mut self.file_system);
                    log_list.redraw(cx);
                }
            }
        }
        
        for action in self.file_system.handle_event(cx, event, &self.ui) {
            match action {
                FileSystemAction::TreeLoaded => {
                    self.open_code_file_by_path(cx, "examples/news_feed/src/app.rs");
                }
                FileSystemAction::RecompileNeeded => {
                    self.build_manager.start_recompile_timer(cx, &self.ui);
                }
                FileSystemAction::LiveReloadNeeded(live_file_change) => {
                    self.build_manager.live_reload_needed(live_file_change);
                    self.build_manager.clear_log(cx, &dock, &mut self.file_system);
                    log_list.redraw(cx);
                }
            }
        }
        
        // lets iterate over the editors and handle events
        for (item_id, item) in dock.borrow_mut().unwrap().visible_items() {
            if let Some(mut run_view) = item.as_run_view().borrow_mut() {
                run_view.handle_event(cx, event, item_id, &mut self.build_manager);
            }
            else if let Some(mut code_editor) = item.as_code_editor().borrow_mut() {
                if let Some(session) = self.file_system.get_session_mut(item_id) {
                    for action in code_editor.handle_event(cx, event, session) {
                        match action {
                            CodeEditorAction::TextDidChange => {
                                // lets write the file
                                self.file_system.request_save_file(item_id)
                            }
                        }
                    }
                }
            }
        }
        
        for action in self.build_manager.handle_event(cx, event, &mut self.file_system, &dock) {
            match action {
                BuildManagerAction::RedrawLog => {
                    // if the log_list is tailing, set the new len
                    log_list.redraw(cx);
                }
                BuildManagerAction::StdinToHost {run_view_id, msg} => {
                    if let Some(mut run_view) = dock.item(run_view_id).as_run_view().borrow_mut() {
                        run_view.handle_stdin_to_host(cx, &msg, run_view_id, &mut self.build_manager);
                    }
                }
                _ => ()
            }
        }
        
        let actions = self.ui.handle_widget_event(cx, event);
        
        for (item_id, item) in run_list.items_with_actions(&actions) {
            for action in self.build_manager.handle_run_list(cx, &run_list, item_id, item, &actions) {
                match action {
                    RunListAction::Create(run_view_id, name) => {
                        let tab_bar_id = dock.find_tab_bar_of_tab(live_id!(run_first)).unwrap();
                        dock.create_and_select_tab(cx, tab_bar_id, run_view_id, live_id!(RunView), name, TabClosable::Yes);
                        dock.redraw(cx);
                    }
                    RunListAction::Destroy(run_view_id) => {
                        dock.close_tab(cx, run_view_id);
                        dock.redraw(cx);
                    }
                    _ => ()
                }
                log_list.redraw(cx);
            }
        }
        
        for (item_id, item) in log_list.items_with_actions(&actions) {
            for action in self.build_manager.handle_log_list(cx, &log_list, item_id, item, &actions) {
                match action {
                    LogListAction::JumpToError{file_name, start, length} => {
                        // lets find a tab if we have it otherwise open it
                        if let Some(file_id) = self.file_system.path_to_file_node_id(&file_name) {
                            if let Some(tab_id) = self.file_system.file_node_id_to_tab_id(file_id){
                                dock.select_tab(cx, tab_id);
                                // ok lets scroll into view
                                if let Some(mut editor) = dock.item(tab_id).as_code_editor().borrow_mut() {
                                    if let Some(session) = self.file_system.get_session_mut(tab_id) {
                                        editor.set_cursor_and_scroll(cx, start, length, session);
                                        editor.set_key_focus(cx);
                                    }
                                }
                            }
                            else{
                                // lets open the editor
                                let tab_id = LiveId::unique();
                                self.file_system.request_open_file(tab_id, file_id);
                                // lets add a file tab 'somewhere'
                                dock.create_and_select_tab(cx, live_id!(edit_tabs), tab_id, live_id!(CodeEditor), "".to_string(), TabClosable::Yes);
                                // lets scan the entire doc for duplicates
                                self.file_system.ensure_unique_tab_names(cx, &dock)
                            }
                        }
                    }
                    _ => ()
                }
                log_list.redraw(cx);
            }
        }
        
        if let Some(tab_id) = dock.clicked_tab_close(&actions) {
            dock.close_tab(cx, tab_id);
            if self.build_manager.handle_tab_close(tab_id) {
                log_list.redraw(cx);
                run_list.redraw(cx);
            }
            self.file_system.remove_tab(tab_id);
            self.file_system.ensure_unique_tab_names(cx, &dock);
        }
        
        if let Some(tab_id) = dock.should_tab_start_drag(&actions) {
            
            dock.tab_start_drag(cx, tab_id, DragItem::FilePath {
                path: "".to_string(), //String::from("file://") + &*path.into_unix_string().to_string_lossy(),
                internal_id: Some(tab_id)
            });
        }
        
        if let Some(drag) = dock.should_accept_drag(&actions) {
            if drag.items.len() == 1 {
                if drag.modifiers.logo {
                    dock.accept_drag(cx, drag, DragResponse::Copy);
                }
                else {
                    dock.accept_drag(cx, drag, DragResponse::Move);
                }
            }
        }
        
        if let Some(drop) = dock.has_drop(&actions) {
            
            if let DragItem::FilePath {path, internal_id} = &drop.items[0] {
                if let Some(internal_id) = internal_id { // from inside the dock
                    if drop.modifiers.logo {
                        dock.drop_clone(cx, drop.abs, *internal_id, LiveId::unique());
                    }
                    else {
                        dock.drop_move(cx, drop.abs, *internal_id);
                    }
                    self.file_system.ensure_unique_tab_names(cx, &dock);
                }
                else { // external file, we have to create a new tab
                    let tab_id = LiveId::unique();
                    if let Some(file_id) = self.file_system.path_to_file_node_id(&path) {
                        self.file_system.request_open_file(tab_id, file_id);
                        dock.drop_create(cx, drop.abs, tab_id, live_id!(CodeEditor), "".to_string(), TabClosable::Yes);
                        self.file_system.ensure_unique_tab_names(cx, &dock)
                    }
                }
            }
        }
        
        if let Some(file_id) = file_tree.should_file_start_drag(&actions) {
            
            let path = self.file_system.file_node_path(file_id);
            file_tree.file_start_drag(cx, file_id, DragItem::FilePath {
                path,
                internal_id: None
            });
        }
        
        if let Some(file_id) = file_tree.file_clicked(&actions) {
            // ok lets open the file
            let tab_id = LiveId::unique();
            self.file_system.request_open_file(tab_id, file_id);
            // lets add a file tab 'somewhere'
            dock.create_and_select_tab(cx, live_id!(edit_tabs), tab_id, live_id!(CodeEditor), "".to_string(), TabClosable::Yes);
            // lets scan the entire doc for duplicates
            self.file_system.ensure_unique_tab_names(cx, &dock)
        }
    }
}