neovide 0.17.2

Neovide: No Nonsense Neovim Gui
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
use objc2::MainThreadOnly;
use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2::runtime::ProtocolObject;
use objc2::runtime::Sel;
use objc2::sel;
use objc2_app_kit::NSApplication;
use objc2_app_kit::NSBezelStyle;
use objc2_app_kit::NSButton;
use objc2_app_kit::NSButtonType;
use objc2_app_kit::NSColor;
use objc2_app_kit::NSControl;
use objc2_app_kit::NSFloatingWindowLevel;
use objc2_app_kit::NSFocusRingType;
use objc2_app_kit::NSFont;
use objc2_app_kit::NSFontWeight;
use objc2_app_kit::NSImage;
use objc2_app_kit::NSImageScaling;
use objc2_app_kit::NSImageView;
use objc2_app_kit::NSLineBreakMode;
use objc2_app_kit::NSResponder;
use objc2_app_kit::NSScrollView;
use objc2_app_kit::NSTextAlignment;
use objc2_app_kit::NSTextField;
use objc2_app_kit::NSTextFieldDelegate;
use objc2_app_kit::NSView;
use objc2_app_kit::NSWindow;
use objc2_app_kit::NSWindowButton;
use objc2_app_kit::NSWindowStyleMask;
use objc2_app_kit::NSWindowTitleVisibility;
use objc2_foundation::MainThreadMarker;
use objc2_foundation::NSInteger;
use objc2_foundation::NSPoint;
use objc2_foundation::NSRect;
use objc2_foundation::NSSize;
use objc2_foundation::NSString;
use objc2_foundation::ns_string;

use super::EDITOR_SWITCHER_STATE;
use super::EditorSwitcherState;
use super::EditorSwitcherStateParts;
use super::appkit::EditorSwitcherActionHandler;
use super::appkit::EditorSwitcherDocumentView;
use super::appkit::EditorSwitcherSearchDelegate;
use super::appkit::EditorSwitcherSearchField;
use super::appkit::EditorSwitcherView;
use super::appkit::EditorSwitcherWindow;
use super::close_editor_switcher;
use super::hotkey::editor_switcher_toggle_shortcut;
use super::layout::BottomBar;
use super::layout::Panel;
use super::layout::Results;
use super::layout::Row;
use super::layout::Search;
use super::row::EditorSwitcherRow;
use crate::window::application::platform::icon::load_neovide_icon;

const SEARCH_PLACEHOLDER: &str = "Search...";
const EMPTY_RESULTS_TEXT: &str = "No matching windows";
const FOOTER_CLOSE_TEXT: &str = "Close";
const FOOTER_OPEN_SELECTED_TEXT: &str = "Open Selected";
const FOOTER_CLOSE_SHORTCUTS: &[&str] = &["esc"];
const FOOTER_OPEN_SELECTED_SHORTCUTS: &[&str] = &["↩"];

const SELECTED_ROW_ALPHA: f64 = 0.10;
const BOTTOM_BAR_ALPHA: f64 = 0.10;
const SHORTCUT_CHIP_ALPHA: f64 = 0.05;

struct FooterShortcutChip {
    view: Retained<NSView>,
    width: f64,
    height: f64,
}

struct EditorSwitcherPanelShell {
    container_view: Retained<NSView>,
    root_view: Retained<EditorSwitcherView>,
}

struct EditorSwitcherResultsArea {
    scroll_view: Retained<NSScrollView>,
    results_container: Retained<EditorSwitcherDocumentView>,
}

struct EditorSwitcherSearchViews {
    field: Retained<NSTextField>,
    delegate: Retained<EditorSwitcherSearchDelegate>,
}

struct FooterButtonSpec {
    text: &'static str,
    shortcuts: &'static [&'static str],
    action: Sel,
}

#[derive(Clone, Copy)]
enum EditorSwitcherLabelStyle {
    Primary,
    Secondary,
    Footer,
    Shortcut,
}

pub fn show_editor_switcher_panel(rows: Vec<EditorSwitcherRow>, custom_icon_path: Option<&String>) {
    let Some(mtm) = MainThreadMarker::new() else {
        log::warn!("Editor switcher requested off the main thread");
        return;
    };

    close_editor_switcher();

    let background = NSColor::controlBackgroundColor();
    let window = editor_switcher_window(mtm, background.as_ref());
    let EditorSwitcherPanelShell { container_view, root_view } =
        editor_switcher_panel_shell(mtm, background.as_ref());

    let EditorSwitcherSearchViews { field: search_field, delegate: search_delegate } =
        editor_switcher_search_field(mtm);
    let root_ns_view: &NSView = root_view.as_ref();
    root_ns_view.addSubview(search_field.as_ref());
    root_ns_view.addSubview(editor_switcher_divider(mtm).as_ref());

    let EditorSwitcherResultsArea { scroll_view, results_container } =
        editor_switcher_results_area(mtm);
    root_ns_view.addSubview(scroll_view.as_ref());

    let row_action_handler = EditorSwitcherActionHandler::new(mtm);
    let bottom_bar_background = editor_switcher_bottom_bar_background();
    root_ns_view.addSubview(
        editor_switcher_bottom_bar(
            mtm,
            row_action_handler.as_ref(),
            bottom_bar_background.as_ref(),
        )
        .as_ref(),
    );

    container_view.addSubview(
        editor_switcher_bottom_bar_gap_fill(mtm, bottom_bar_background.as_ref()).as_ref(),
    );
    root_ns_view.setFrame(NSRect::new(
        NSPoint::new(0.0, Panel::TITLE_ADJUSTMENT),
        NSSize::new(Panel::WIDTH, Panel::CONTENT_HEIGHT),
    ));
    container_view.addSubview(root_ns_view);
    window.setContentView(Some(container_view.as_ref()));

    let icon = load_neovide_icon(custom_icon_path);
    let state = EditorSwitcherState::new(EditorSwitcherStateParts {
        window,
        search_field,
        results_container,
        row_action_handler,
        search_delegate,
        rows,
        icon,
        toggle_shortcut: editor_switcher_toggle_shortcut(),
    });

    present_editor_switcher(mtm, state);
}

pub fn rebuild_editor_switcher_rows(state: &EditorSwitcherState) {
    let Some(mtm) = MainThreadMarker::new() else {
        return;
    };

    let container: &NSView = state.results_container.as_ref();
    for subview in container.subviews().iter() {
        subview.removeFromSuperview();
    }

    let visible_width = Panel::WIDTH;
    let scroll_view_height = Results::CONTAINER_HEIGHT;

    if state.filtered_indices.is_empty() {
        container.setFrameSize(NSSize::new(visible_width, scroll_view_height));
        let empty_label = editor_switcher_empty_results_label(mtm, visible_width);
        container.addSubview(empty_label.as_ref());
        return;
    }

    let document_height = (state.filtered_indices.len() as f64 * Row::HEIGHT
        + Results::BOTTOM_PADDING)
        .max(scroll_view_height);
    container.setFrameSize(NSSize::new(visible_width, document_height));

    for (row_index, row_data_index) in state.filtered_indices.iter().copied().enumerate() {
        let row = &state.rows[row_data_index];
        let selected = row_index == state.selected_index;
        let y = row_index as f64 * Row::HEIGHT;
        let row_view = editor_switcher_row_button(
            mtm,
            row,
            row_index,
            selected,
            state.icon.as_deref(),
            state.row_action_handler.as_ref(),
        );
        let row_ns_view: &NSView = row_view.as_ref();
        row_ns_view.setFrame(NSRect::new(
            NSPoint::new(Results::OUTER_PADDING, y),
            NSSize::new(visible_width - Results::OUTER_PADDING * 2.0, Row::BUTTON_HEIGHT),
        ));
        container.addSubview(row_view.as_ref());
    }

    scroll_selected_editor_switcher_row_into_view(container, state.selected_index);
}

fn present_editor_switcher(mtm: MainThreadMarker, state: EditorSwitcherState) {
    state.update_results_view();

    let app = NSApplication::sharedApplication(mtm);
    #[allow(deprecated)]
    app.activateIgnoringOtherApps(true);
    let switcher_window = state.window.clone();
    let switcher_search_field = state.search_field.clone();
    switcher_window.makeKeyAndOrderFront(None);

    EDITOR_SWITCHER_STATE.with(|cell| {
        *cell.borrow_mut() = Some(state);
    });

    let search_view: &NSView = switcher_search_field.as_ref();
    let responder: &NSResponder = search_view.as_ref();
    switcher_window.makeFirstResponder(Some(responder));
}

fn editor_switcher_window(mtm: MainThreadMarker, background: &NSColor) -> Retained<NSWindow> {
    let style = NSWindowStyleMask::Titled
        | NSWindowStyleMask::Closable
        | NSWindowStyleMask::FullSizeContentView;
    let window: Retained<NSWindow> =
        EditorSwitcherWindow::new(mtm, editor_switcher_panel_frame(), style).into_super();

    window.setTitle(ns_string!("Editors"));
    window.setTitleVisibility(NSWindowTitleVisibility::Hidden);
    window.setTitlebarAppearsTransparent(true);
    window.setLevel(NSFloatingWindowLevel);
    window.setHasShadow(true);
    window.setMovableByWindowBackground(true);
    window.setOpaque(false);
    window.setBackgroundColor(Some(background));
    hide_editor_switcher_window_buttons(&window);

    unsafe {
        window.setReleasedWhenClosed(false);
    }

    window.center();

    window
}

fn editor_switcher_panel_shell(
    mtm: MainThreadMarker,
    background: &NSColor,
) -> EditorSwitcherPanelShell {
    let root_view = EditorSwitcherView::new(mtm, editor_switcher_content_frame());
    let root_ns_view: &NSView = root_view.as_ref();
    set_editor_switcher_view_background(root_ns_view, background, 0.0);

    let container_view = NSView::initWithFrame(NSView::alloc(mtm), editor_switcher_panel_frame());
    set_editor_switcher_view_background(container_view.as_ref(), background, 0.0);

    EditorSwitcherPanelShell { container_view, root_view }
}

fn editor_switcher_search_field(mtm: MainThreadMarker) -> EditorSwitcherSearchViews {
    let search_frame = NSRect::new(
        NSPoint::new(
            Panel::HORIZONTAL_PADDING,
            Search::VERTICAL_PADDING + Search::FIELD_TOP_OFFSET,
        ),
        NSSize::new(
            Panel::WIDTH - Panel::HORIZONTAL_PADDING * 2.0,
            Search::FONT_SIZE + Search::FIELD_HEIGHT_EXTRA,
        ),
    );
    let search_field: Retained<NSTextField> =
        EditorSwitcherSearchField::new(mtm, search_frame).into_super();
    let search_placeholder = NSString::from_str(SEARCH_PLACEHOLDER);
    search_field.setPlaceholderString(Some(&search_placeholder));
    search_field.setBezeled(false);
    search_field.setBordered(false);
    search_field.setDrawsBackground(false);
    search_field.setEditable(true);
    search_field.setSelectable(true);
    search_field.setTextColor(Some(NSColor::labelColor().as_ref()));

    let search_view: &NSView = search_field.as_ref();
    search_view.setFocusRingType(NSFocusRingType::None);

    let search_control: &NSControl = search_field.as_ref();
    search_control.setFont(Some(NSFont::systemFontOfSize(Search::FONT_SIZE).as_ref()));
    search_control.setRefusesFirstResponder(false);

    let search_delegate = EditorSwitcherSearchDelegate::new(mtm);
    unsafe {
        let search_delegate_object: &EditorSwitcherSearchDelegate = search_delegate.as_ref();
        let search_delegate_protocol: &ProtocolObject<dyn NSTextFieldDelegate> =
            ProtocolObject::from_ref(search_delegate_object);
        search_field.setDelegate(Some(search_delegate_protocol));
    }

    EditorSwitcherSearchViews { field: search_field, delegate: search_delegate }
}

fn editor_switcher_divider(mtm: MainThreadMarker) -> Retained<NSView> {
    let divider = NSView::initWithFrame(
        NSView::alloc(mtm),
        NSRect::new(NSPoint::new(0.0, Search::SECTION_HEIGHT), NSSize::new(Panel::WIDTH, 1.0)),
    );

    set_editor_switcher_view_background(divider.as_ref(), NSColor::separatorColor().as_ref(), 0.0);
    divider
}

fn editor_switcher_results_area(mtm: MainThreadMarker) -> EditorSwitcherResultsArea {
    let scroll_y = Search::SECTION_HEIGHT + Results::OUTER_PADDING;
    let scroll_height = Results::CONTAINER_HEIGHT;
    let scroll_view = NSScrollView::initWithFrame(
        NSScrollView::alloc(mtm),
        NSRect::new(NSPoint::new(0.0, scroll_y), NSSize::new(Panel::WIDTH, scroll_height)),
    );
    scroll_view.setDrawsBackground(false);
    scroll_view.setBorderType(objc2_app_kit::NSBorderType::NoBorder);
    scroll_view.setHasVerticalScroller(true);
    scroll_view.setHasHorizontalScroller(false);
    scroll_view.setAutohidesScrollers(true);

    let results_container = EditorSwitcherDocumentView::new(
        mtm,
        NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(Panel::WIDTH, scroll_height)),
    );

    let results_view: &NSView = results_container.as_ref();
    scroll_view.setDocumentView(Some(results_view));

    EditorSwitcherResultsArea { scroll_view, results_container }
}

fn editor_switcher_bottom_bar(
    mtm: MainThreadMarker,
    row_action_handler: &EditorSwitcherActionHandler,
    background: &NSColor,
) -> Retained<NSView> {
    let bottom_bar_height = editor_switcher_bottom_bar_height();
    let bottom_bar = NSView::initWithFrame(
        NSView::alloc(mtm),
        NSRect::new(
            NSPoint::new(0.0, editor_switcher_bottom_bar_y()),
            NSSize::new(Panel::WIDTH, bottom_bar_height),
        ),
    );
    set_editor_switcher_view_background(bottom_bar.as_ref(), background, 0.0);

    let mut trailing_edge = Panel::WIDTH - BottomBar::HORIZONTAL_PADDING;
    for spec in editor_switcher_footer_buttons_from_trailing_edge() {
        let button = editor_switcher_bottom_bar_button(mtm, spec.text, spec.shortcuts, spec.action);
        let button_view: &NSView = button.as_ref();
        let button_size = button_view.frame().size;

        trailing_edge -= button_size.width;
        button_view.setFrame(NSRect::new(
            NSPoint::new(trailing_edge, (bottom_bar_height - button_size.height) / 2.0),
            button_size,
        ));
        trailing_edge -= BottomBar::BUTTON_SPACING;

        let button_control: &NSControl = button.as_ref();
        set_editor_switcher_control_target(button_control, row_action_handler);
        bottom_bar.addSubview(button.as_ref());
    }

    bottom_bar
}

fn editor_switcher_bottom_bar_background() -> Retained<NSColor> {
    NSColor::blackColor().colorWithAlphaComponent(BOTTOM_BAR_ALPHA)
}

fn editor_switcher_footer_buttons_from_trailing_edge() -> [FooterButtonSpec; 2] {
    [
        FooterButtonSpec {
            text: FOOTER_CLOSE_TEXT,
            shortcuts: FOOTER_CLOSE_SHORTCUTS,
            action: sel!(editorSwitcherClose:),
        },
        FooterButtonSpec {
            text: FOOTER_OPEN_SELECTED_TEXT,
            shortcuts: FOOTER_OPEN_SELECTED_SHORTCUTS,
            action: sel!(editorSwitcherOpenSelected:),
        },
    ]
}

fn editor_switcher_bottom_bar_gap_fill(
    mtm: MainThreadMarker,
    background: &NSColor,
) -> Retained<NSView> {
    let bottom_bar_gap_fill = NSView::initWithFrame(
        NSView::alloc(mtm),
        NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(Panel::WIDTH, Panel::TITLE_ADJUSTMENT)),
    );
    set_editor_switcher_view_background(bottom_bar_gap_fill.as_ref(), background, 0.0);

    bottom_bar_gap_fill
}

fn editor_switcher_panel_frame() -> NSRect {
    NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(Panel::WIDTH, Panel::HEIGHT))
}

fn editor_switcher_content_frame() -> NSRect {
    NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(Panel::WIDTH, Panel::CONTENT_HEIGHT))
}

fn editor_switcher_bottom_bar_y() -> f64 {
    Search::SECTION_HEIGHT + Results::OUTER_PADDING + Results::CONTAINER_HEIGHT
}

fn editor_switcher_bottom_bar_height() -> f64 {
    (Panel::CONTENT_HEIGHT - editor_switcher_bottom_bar_y()).max(BottomBar::HEIGHT)
}

fn editor_switcher_empty_results_label(
    mtm: MainThreadMarker,
    visible_width: f64,
) -> Retained<NSTextField> {
    let empty_label = editor_switcher_label(
        mtm,
        EMPTY_RESULTS_TEXT,
        Results::EMPTY_LABEL_FONT_SIZE,
        EditorSwitcherLabelStyle::Secondary,
    );
    let empty_label_view: &NSView = empty_label.as_ref();
    empty_label_view.setFrame(NSRect::new(
        NSPoint::new(Panel::HORIZONTAL_PADDING, Results::EMPTY_LABEL_Y),
        NSSize::new(visible_width - Panel::HORIZONTAL_PADDING * 2.0, Results::EMPTY_LABEL_HEIGHT),
    ));
    empty_label
}

fn scroll_selected_editor_switcher_row_into_view(container: &NSView, selected_index: usize) {
    let visible_rect = container.visibleRect();
    let visible_min_y = visible_rect.origin.y;
    let visible_max_y = visible_rect.origin.y + visible_rect.size.height;
    let selected_min_y = selected_index as f64 * Row::HEIGHT;
    let selected_max_y = selected_min_y + Row::HEIGHT;

    let scroll_y = if selected_min_y < visible_min_y {
        selected_min_y
    } else if selected_max_y > visible_max_y {
        selected_max_y - visible_rect.size.height
    } else {
        return;
    };

    container.scrollPoint(NSPoint::new(0.0, scroll_y.max(0.0)));
}

fn hide_editor_switcher_window_buttons(window: &NSWindow) {
    for button_kind in
        [NSWindowButton::MiniaturizeButton, NSWindowButton::CloseButton, NSWindowButton::ZoomButton]
    {
        if let Some(button) = window.standardWindowButton(button_kind) {
            let button_view: &NSView = button.as_ref();
            button_view.setHidden(true);
        }
    }
}

fn editor_switcher_row_frame(row_width: f64, row_height: f64) -> NSRect {
    NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(row_width, row_height))
}

fn editor_switcher_row_icon_frame(row_height: f64) -> NSRect {
    NSRect::new(
        NSPoint::new(Results::INNER_PADDING, (row_height - Row::ICON_SIZE) / 2.0),
        NSSize::new(Row::ICON_SIZE, Row::ICON_SIZE),
    )
}

fn editor_switcher_row_title_frame(row_width: f64, row_height: f64) -> NSRect {
    let title_x = Results::INNER_PADDING + Row::ICON_SIZE + Row::ICON_TITLE_SPACING;
    NSRect::new(
        NSPoint::new(title_x, (row_height - Row::LABEL_HEIGHT) / 2.0),
        NSSize::new(row_width - title_x - Results::INNER_PADDING, Row::LABEL_HEIGHT),
    )
}

fn editor_switcher_row_button(
    mtm: MainThreadMarker,
    row: &EditorSwitcherRow,
    row_index: usize,
    selected: bool,
    icon: Option<&NSImage>,
    row_action_handler: &EditorSwitcherActionHandler,
) -> Retained<NSButton> {
    let row_width = Panel::WIDTH - Results::OUTER_PADDING * 2.0;
    let row_height = Row::BUTTON_HEIGHT;
    let button = editor_switcher_button(mtm, editor_switcher_row_frame(row_width, row_height));

    let row_control: &NSControl = button.as_ref();
    row_control.setTag(row_index as NSInteger);
    row_control.setRefusesFirstResponder(true);
    set_editor_switcher_control_target(row_control, row_action_handler);
    unsafe {
        row_control.setAction(Some(sel!(editorSwitcherRowClicked:)));
    }

    let row_view: &NSView = button.as_ref();
    let row_background = if selected {
        NSColor::systemGrayColor().colorWithAlphaComponent(SELECTED_ROW_ALPHA)
    } else {
        NSColor::clearColor()
    };
    set_editor_switcher_view_background(row_view, &row_background, Row::CORNER_RADIUS);

    if let Some(icon) = icon {
        let image_view = NSImageView::initWithFrame(
            NSImageView::alloc(mtm),
            editor_switcher_row_icon_frame(row_height),
        );
        image_view.setImage(Some(icon));
        image_view.setImageScaling(NSImageScaling::ScaleProportionallyDown);
        row_view.addSubview(image_view.as_ref());
    }

    let title_label = editor_switcher_label(
        mtm,
        &row.title,
        Row::TITLE_FONT_SIZE,
        EditorSwitcherLabelStyle::Primary,
    );
    let title_label_view: &NSView = title_label.as_ref();
    title_label_view.setFrame(editor_switcher_row_title_frame(row_width, row_height));
    row_view.addSubview(title_label.as_ref());

    button
}

fn editor_switcher_bottom_bar_button(
    mtm: MainThreadMarker,
    text: &str,
    shortcuts: &[&str],
    action: Sel,
) -> Retained<NSButton> {
    let text_label =
        editor_switcher_label(mtm, text, BottomBar::FONT_SIZE, EditorSwitcherLabelStyle::Footer);
    let text_size = editor_switcher_fit_label(text_label.as_ref());
    let text_width = text_size.width.ceil();
    let text_height = text_size.height.ceil();

    let shortcut_chips: Vec<FooterShortcutChip> = shortcuts
        .iter()
        .map(|shortcut| {
            editor_switcher_footer_shortcut_chip(mtm, shortcut, BottomBar::SHORTCUT_FONT_SIZE)
        })
        .collect();
    let shortcuts_width = shortcut_chips.iter().map(|chip| chip.width).sum::<f64>()
        + BottomBar::SHORTCUT_SPACING * shortcut_chips.len().saturating_sub(1) as f64;
    let shortcuts_height = shortcut_chips.iter().map(|chip| chip.height).fold(0.0, f64::max);
    let label_shortcut_spacing =
        if shortcut_chips.is_empty() { 0.0 } else { BottomBar::LABEL_SHORTCUT_SPACING };
    let content_height = text_height.max(shortcuts_height);
    let button_height =
        BottomBar::BUTTON_HEIGHT.max(content_height + BottomBar::BUTTON_VERTICAL_PADDING * 2.0);
    let width = BottomBar::BUTTON_LEADING_PADDING
        + text_width
        + label_shortcut_spacing
        + shortcuts_width
        + BottomBar::BUTTON_TRAILING_PADDING;

    let button = editor_switcher_button(
        mtm,
        NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(width, button_height)),
    );
    let button_control: &NSControl = button.as_ref();
    button_control.setRefusesFirstResponder(true);
    unsafe {
        button_control.setAction(Some(action));
    }

    let button_view: &NSView = button.as_ref();
    set_editor_switcher_view_background(
        button_view,
        &NSColor::clearColor(),
        BottomBar::BUTTON_CORNER_RADIUS,
    );

    let text_label_view: &NSView = text_label.as_ref();
    text_label_view.setFrame(NSRect::new(
        NSPoint::new(BottomBar::BUTTON_LEADING_PADDING, (button_height - text_height) / 2.0),
        NSSize::new(text_width, text_height),
    ));
    button_view.addSubview(text_label.as_ref());

    let mut x = BottomBar::BUTTON_LEADING_PADDING + text_width + label_shortcut_spacing;
    for shortcut_chip in shortcut_chips {
        let shortcut_chip_view: &NSView = shortcut_chip.view.as_ref();
        shortcut_chip_view
            .setFrameOrigin(NSPoint::new(x, (button_height - shortcut_chip.height) / 2.0));
        button_view.addSubview(shortcut_chip.view.as_ref());
        x += shortcut_chip.width + BottomBar::SHORTCUT_SPACING;
    }

    button
}

fn editor_switcher_footer_shortcut_chip(
    mtm: MainThreadMarker,
    text: &str,
    size: f64,
) -> FooterShortcutChip {
    let label = editor_switcher_label(mtm, text, size, EditorSwitcherLabelStyle::Shortcut);
    let label_size = editor_switcher_fit_label(label.as_ref());
    let label_width = label_size.width.ceil();
    let label_height = label_size.height.ceil();
    let width = label_width + BottomBar::SHORTCUT_HORIZONTAL_PADDING * 2.0;
    let height = label_height + BottomBar::SHORTCUT_VERTICAL_PADDING * 2.0;

    let chip = NSView::initWithFrame(
        NSView::alloc(mtm),
        NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(width, height)),
    );
    set_editor_switcher_view_background(
        chip.as_ref(),
        &NSColor::whiteColor().colorWithAlphaComponent(SHORTCUT_CHIP_ALPHA),
        BottomBar::SHORTCUT_CHIP_CORNER_RADIUS,
    );

    let label_view: &NSView = label.as_ref();
    label_view.setFrame(NSRect::new(
        NSPoint::new(BottomBar::SHORTCUT_HORIZONTAL_PADDING, BottomBar::SHORTCUT_VERTICAL_PADDING),
        NSSize::new(label_width, label_height),
    ));
    chip.addSubview(label.as_ref());

    FooterShortcutChip { view: chip, width, height }
}

fn editor_switcher_button(mtm: MainThreadMarker, frame: NSRect) -> Retained<NSButton> {
    let button = NSButton::initWithFrame(NSButton::alloc(mtm), frame);
    button.setTitle(ns_string!(""));
    button.setButtonType(NSButtonType::MomentaryChange);
    button.setBezelStyle(NSBezelStyle::Automatic);
    button.setBordered(false);
    button.setTransparent(true);
    button
}

fn set_editor_switcher_control_target(control: &NSControl, target: &EditorSwitcherActionHandler) {
    unsafe {
        let target: &AnyObject = target.as_ref();
        control.setTarget(Some(target));
    }
}

fn editor_switcher_fit_label(label: &NSTextField) -> NSSize {
    let control: &NSControl = label.as_ref();
    control.sizeToFit();
    let label_view: &NSView = label.as_ref();
    label_view.frame().size
}

fn editor_switcher_label(
    mtm: MainThreadMarker,
    text: &str,
    size: f64,
    style: EditorSwitcherLabelStyle,
) -> Retained<NSTextField> {
    let label = NSTextField::labelWithString(&NSString::from_str(text), mtm);
    let control: &NSControl = label.as_ref();
    let font = match style {
        EditorSwitcherLabelStyle::Shortcut => {
            NSFont::monospacedSystemFontOfSize_weight(size, NSFontWeight::from(0))
        }
        _ => NSFont::systemFontOfSize(size),
    };
    control.setFont(Some(font.as_ref()));
    if matches!(style, EditorSwitcherLabelStyle::Shortcut) {
        control.setAlignment(NSTextAlignment(2));
    }
    control.setLineBreakMode(NSLineBreakMode::ByTruncatingTail);
    control.setUsesSingleLineMode(true);
    let text_color = match style {
        EditorSwitcherLabelStyle::Primary => NSColor::labelColor(),
        EditorSwitcherLabelStyle::Secondary => NSColor::secondaryLabelColor(),
        EditorSwitcherLabelStyle::Footer | EditorSwitcherLabelStyle::Shortcut => {
            NSColor::systemGrayColor()
        }
    };
    label.setTextColor(Some(text_color.as_ref()));
    label.setMaximumNumberOfLines(1);
    label
}

fn set_editor_switcher_view_background(view: &NSView, color: &NSColor, corner_radius: f64) {
    view.setWantsLayer(true);
    if let Some(layer) = view.layer() {
        let cg_color = color.CGColor();
        layer.setBackgroundColor(Some(cg_color.as_ref()));
        layer.setCornerRadius(corner_radius);
        layer.setMasksToBounds(true);
    }
}