Repository navigation
Add desktop rail workspace and server switcher #1034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e8db247
212191a
51282ad
3c1ec24
28c6840
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,12 @@ use super::phone::tokens; | |
| use super::people::presence; | ||
| use super::sidebar::Mode; | ||
| use super::state::{Shell, Snapshot}; | ||
| use super::theme; | ||
| use super::theme::{self, Type}; | ||
| use gpui::{ | ||
| div, prelude::*, px, AnyElement, Context, Div, ElementId, MouseButton, SharedString, Stateful, | ||
| div, prelude::*, px, AnyElement, Bounds, Context, Div, ElementId, MouseButton, Pixels, SharedString, Stateful, | ||
| Window, | ||
| }; | ||
| use std::time::Duration; | ||
| use protocol::{Link, Network}; | ||
|
|
||
| /// What a rail button's tooltip says after its name. | ||
|
|
@@ -25,6 +26,22 @@ fn waiting(attention: u32, unread: bool) -> String { | |
| } | ||
| } | ||
|
|
||
| /// Servers a rail list shows before it says how many more there are. | ||
| const FLYOUT_SHOWN: usize = 5; | ||
|
|
||
| /// A list of servers hanging off a rail icon. `button` and `list`: the pointer is on the icon, on | ||
| /// the list. `shown`: the beat before it appears has passed. | ||
| #[derive(Clone, Debug)] | ||
| pub struct Flyout { | ||
| pub network: Network, | ||
| pub at: Bounds<Pixels>, | ||
| pub button: bool, | ||
| pub list: bool, | ||
| pub shown: bool, | ||
| pub keyboard: bool, | ||
| pub opener: Option<gpui::FocusHandle>, | ||
| } | ||
|
|
||
| /// One button of the rail. | ||
| struct Place { | ||
| mode: Mode, | ||
|
|
@@ -57,10 +74,15 @@ impl Shell { | |
| let phone = super::phone::active(); | ||
| const KEYS: [&str; 9] = ["⌘1", "⌘2", "⌘3", "⌘4", "⌘5", "⌘6", "⌘7", "⌘8", "⌘9"]; | ||
| let label = format!("{name}{}", waiting(attention, unread)); | ||
| let flies = !phone | ||
| && matches!(mode, Mode::Network(net @ (Network::Slack | Network::Mattermost | Network::Discord)) | ||
| if self.spaces.iter().filter(|s| s.key.network == net).count() >= if net == Network::Discord { 1 } else { 2 }); | ||
| let flyout_open = matches!(mode, Mode::Network(network) if self.layout_state.flyout.as_ref().is_some_and(|flyout| flyout.network == network)); | ||
| div() | ||
| .id(id) | ||
| .role(gpui::Role::Button) | ||
| .aria_label(label.clone()) | ||
| .aria_expanded(flyout_open) | ||
| .when(lit, |row| row.aria_description("Current place")) | ||
| .tab_index(0) | ||
| .focus_visible(|style| style.shadow(theme::focus_ring_inset())) | ||
|
|
@@ -75,7 +97,19 @@ impl Shell { | |
| .when(lit, |button| button.bg(theme::selected())) | ||
| .when(!lit && !phone, |button| button.hover(|style| style.bg(theme::hover()))) | ||
| .when(phone, |button| button.active(|style| style.bg(theme::pressed()))) | ||
| .tooltip(tip(label, KEYS.get(place).copied())) | ||
| .when(!flies, |button| button.tooltip(tip(label, KEYS.get(place).copied()))) | ||
| .when_some(flies.then_some(mode.clone()), |button, mode| { | ||
| let Mode::Network(network) = mode else { return button }; | ||
| button | ||
| .track_focus(self.layout_state.rail_focus.get(&network).expect("eligible network has a rail focus handle")) | ||
| .on_hover(cx.listener(move |this, over: &bool, window, cx| this.rail_hover(network, *over, window, cx))) | ||
| .on_key_down(cx.listener(move |this, event: &gpui::KeyDownEvent, window, cx| { | ||
| if event.keystroke.key == "down" { | ||
| cx.stop_propagation(); | ||
| this.rail_keyboard_open(network, window, cx); | ||
| } | ||
| })) | ||
| }) | ||
| .on_click(cx.listener(move |this, _, window, cx| this.visit(mode.clone(), window, cx))) | ||
| .child(div().when(faint, |content| content.opacity(0.4)).child(content)) | ||
| .children(rail_badge(attention, unread, mention, counted)) | ||
|
|
@@ -94,6 +128,211 @@ impl Shell { | |
| }) | ||
| } | ||
|
|
||
| /// Open the flyout from the keyboard when its rail button is focused. Centering the anchor in | ||
| /// the rail keeps it visible even when the focused button is in a scrolled server list. | ||
| fn rail_keyboard_open(&mut self, network: Network, window: &mut Window, cx: &mut Context<Self>) { | ||
| let y = f32::from(window.viewport_size().height) / 2. - 22.; | ||
| let at = super::components::place::anchor(0., y.max(0.), super::layout::RAIL_WIDTH, 44.); | ||
| let opener = self.layout_state.rail_focus.get(&network).cloned(); | ||
| self.layout_state.flyout = Some(Flyout { network, at, button: false, list: false, shown: true, keyboard: true, opener }); | ||
| window.focus(&self.layout_state.flyout_focus, cx); | ||
| self.rail_flyout_lapse(network, window.window_handle(), Duration::from_millis(200), cx); | ||
| cx.notify(); | ||
| } | ||
|
|
||
| /// The pointer came to rest on a network's rail icon, or left it: after a beat the list of | ||
| /// that network's servers or workspaces hangs off the icon, and goes when the pointer has | ||
| /// left both. | ||
| fn rail_hover(&mut self, network: Network, over: bool, window: &mut Window, cx: &mut Context<Self>) { | ||
| const WAIT: Duration = Duration::from_millis(250); | ||
| const GRACE: Duration = Duration::from_millis(200); | ||
| if over { | ||
| let y = f32::from(window.mouse_position().y) - 22.; | ||
| let at = super::components::place::anchor(0., y.max(0.), super::layout::RAIL_WIDTH, 44.); | ||
| let shown = self.layout_state.flyout.as_ref().is_some_and(|f| f.shown); | ||
| self.layout_state.flyout = Some(Flyout { network, at, button: true, list: false, shown, keyboard: false, opener: None }); | ||
| cx.spawn(async move |this, cx| { | ||
| cx.background_executor().timer(if shown { Duration::ZERO } else { WAIT }).await; | ||
| let _ = this.update(cx, |this, cx| { | ||
| if let Some(flyout) = this.layout_state.flyout.as_mut().filter(|f| f.network == network && f.button) { | ||
| flyout.shown = true; | ||
| cx.notify(); | ||
| } | ||
| }); | ||
| }) | ||
| .detach(); | ||
| } else { | ||
| if let Some(flyout) = self.layout_state.flyout.as_mut().filter(|f| f.network == network) { | ||
| flyout.button = false; | ||
| } | ||
| self.rail_flyout_lapse(network, window.window_handle(), GRACE, cx); | ||
| } | ||
| } | ||
|
|
||
| /// After a grace, drop the flyout if the pointer is on neither the icon nor the list. | ||
| fn rail_flyout_lapse(&mut self, network: Network, window: gpui::AnyWindowHandle, grace: Duration, cx: &mut Context<Self>) { | ||
| cx.spawn(async move |this, cx| { | ||
| cx.background_executor().timer(grace).await; | ||
| let _ = cx.update_window(window, |_, window, cx| { | ||
| let _ = this.update(cx, |this, cx| { | ||
| if let Some(flyout) = this.layout_state.flyout.as_ref().filter(|f| f.network == network && !f.button && !f.list) { | ||
| if flyout.keyboard && this.layout_state.flyout_focus.contains_focused(window, cx) { | ||
| this.rail_flyout_lapse(network, window.window_handle(), Duration::from_millis(200), cx); | ||
| return; | ||
| } | ||
| this.layout_state.flyout = None; | ||
| cx.notify(); | ||
| } | ||
| }); | ||
| }); | ||
| }) | ||
| .detach(); | ||
| } | ||
|
|
||
| /// The list that hangs off a rail icon: the network's servers (Discord, Mattermost) or | ||
| /// workspaces (Slack), the current one marked. Picking one goes there. | ||
| pub(super) fn rail_flyout_layer(&self, window: &mut Window, cx: &mut Context<Self>) -> Option<AnyElement> { | ||
| let flyout = self.layout_state.flyout.as_ref().filter(|f| f.shown)?; | ||
| let network = flyout.network; | ||
| let here: Vec<&protocol::Space> = self.spaces.iter().filter(|s| s.key.network == network).collect(); | ||
| let single = network != Network::Discord; | ||
| if here.len() < if single { 2 } else { 1 } { | ||
| return None; | ||
| } | ||
| let current = match (&self.sidebar_state.mode, single) { | ||
| (Mode::Space(key), _) => Some(key.clone()), | ||
| (_, true) => self.workspace_space(network), | ||
| _ => None, | ||
| }; | ||
| let mut menu = super::components::menu().w(px(260.)).child(super::components::menu_label(format!("{} · {}", network.label(), if network == Network::Slack { "Workspaces" } else { "Servers" }))); | ||
| let more = here.len().saturating_sub(FLYOUT_SHOWN); | ||
| for (ix, space) in here.iter().take(FLYOUT_SHOWN).enumerate() { | ||
| let key = space.key.clone(); | ||
| let click_key = key.clone(); | ||
| let keyboard_key = key.clone(); | ||
| let icon = self.media_path(&space.key, space.icon.as_ref()); | ||
| menu = menu.child( | ||
| div() | ||
| .id(("rail-flyout-row", ix)) | ||
| .role(gpui::Role::MenuItem) | ||
| .aria_label(space.name.clone()) | ||
| .tab_index(0) | ||
| .focus_visible(|style| style.shadow(theme::focus_ring_inset())) | ||
| .h(px(28.)) | ||
| .px(px(8.)) | ||
| .mx(px(4.)) | ||
| .rounded(px(theme::R_CONTROL)) | ||
| .flex() | ||
| .items_center() | ||
| .gap(px(8.)) | ||
| .cursor_pointer() | ||
| .when(current.as_ref() == Some(&space.key), |row| row.bg(theme::selected())) | ||
| .hover(|style| style.bg(theme::hover())) | ||
| .on_click(cx.listener(move |this, _, window, cx| { | ||
| this.choose_rail_space(network, click_key.clone(), window, cx); | ||
| })) | ||
| .on_key_down(cx.listener(move |this, event: &gpui::KeyDownEvent, window, cx| { | ||
| if matches!(event.keystroke.key.as_str(), "enter" | "space") { | ||
| cx.stop_propagation(); | ||
| this.choose_rail_space(network, keyboard_key.clone(), window, cx); | ||
| } | ||
| })) | ||
| .child(Avatar::new(&space.key, &space.name, 20.).shape(Shape::Place).image_at(icon).build()) | ||
| .child(div().flex_1().min_w_0().truncate().type_ui().text_color(theme::text()).child(space.name.clone())) | ||
| .children(super::components::rollup_badge(space.attention, space.unread)), | ||
| ); | ||
| } | ||
| if more > 0 { | ||
| menu = menu.child( | ||
| div() | ||
| .id("rail-flyout-more") | ||
| .role(gpui::Role::MenuItem) | ||
| .aria_label(format!("Show all {}", network.label())) | ||
| .tab_index(0) | ||
| .focus_visible(|style| style.shadow(theme::focus_ring_inset())) | ||
| .h(px(28.)) | ||
| .px(px(8.)) | ||
| .mx(px(4.)) | ||
| .rounded(px(theme::R_CONTROL)) | ||
| .flex() | ||
| .items_center() | ||
| .gap(px(8.)) | ||
| .cursor_pointer() | ||
| .hover(|style| style.bg(theme::hover())) | ||
| .on_click(cx.listener(move |this, _, window, cx| { | ||
| this.show_all_rail_spaces(network, window, cx); | ||
| })) | ||
| .on_key_down(cx.listener(move |this, event: &gpui::KeyDownEvent, window, cx| { | ||
| if matches!(event.keystroke.key.as_str(), "enter" | "space") { | ||
| cx.stop_propagation(); | ||
| this.show_all_rail_spaces(network, window, cx); | ||
| } | ||
| })) | ||
| .child(div().size(px(20.)).flex_none().flex().items_center().justify_center().child(glyph("icons/ellipsis.svg", 14., theme::text_faint()))) | ||
| .child(div().flex_1().min_w_0().type_ui().text_color(theme::text_soft()).child(format!("{more} more"))) | ||
| .child(glyph("icons/chevron-right.svg", 12., theme::text_faint())), | ||
| ); | ||
| } | ||
| let menu = menu | ||
| .id("rail-flyout-list") | ||
| .role(gpui::Role::Menu) | ||
| .aria_label(format!("{} {}", network.label(), if network == Network::Slack { "workspaces" } else { "servers" })) | ||
| .tab_index(0) | ||
| .track_focus(&self.layout_state.flyout_focus) | ||
| .on_key_down(cx.listener(|this, event: &gpui::KeyDownEvent, window, cx| { | ||
| match event.keystroke.key.as_str() { | ||
| "down" => window.focus_next(cx), | ||
| "up" => window.focus_prev(cx), | ||
| "escape" => { | ||
| let opener = this.layout_state.flyout.take().and_then(|flyout| flyout.opener); | ||
| if let Some(opener) = opener.as_ref() { | ||
| window.focus(opener, cx); | ||
| } | ||
| cx.notify(); | ||
| } | ||
| _ => return, | ||
| } | ||
| cx.stop_propagation(); | ||
| cx.notify(); | ||
| })) | ||
| .on_hover(cx.listener(move |this, over: &bool, window, cx| { | ||
| if let Some(flyout) = this.layout_state.flyout.as_mut().filter(|flyout| flyout.network == network) { | ||
| flyout.list = *over; | ||
| } | ||
| if !*over { | ||
| this.rail_flyout_lapse(network, window.window_handle(), Duration::from_millis(200), cx); | ||
| } | ||
| })); | ||
| Some(super::components::placed("rail-flyout", super::components::Place::Beside(flyout.at), super::components::Layer::Popover, window, menu)) | ||
| } | ||
|
|
||
| fn close_rail_flyout(&mut self, window: &mut Window, cx: &mut Context<Self>) { | ||
| let opener = self.layout_state.flyout.take().and_then(|flyout| flyout.keyboard.then_some(flyout.opener).flatten()); | ||
| if let Some(opener) = opener.as_ref() { | ||
| window.focus(opener, cx); | ||
| } | ||
| } | ||
|
|
||
| fn choose_rail_space(&mut self, network: Network, key: protocol::Key, window: &mut Window, cx: &mut Context<Self>) { | ||
| self.close_rail_flyout(window, cx); | ||
| if network == Network::Discord { | ||
| self.visit(Mode::Space(key), window, cx); | ||
| } else { | ||
| match network { | ||
| Network::Slack => self.sidebar_state.slack = Some(key), | ||
| Network::Mattermost => self.sidebar_state.mattermost = Some(key), | ||
| _ => return, | ||
| } | ||
| self.visit(Mode::Network(network), window, cx); | ||
| self.show_mode(cx); | ||
| } | ||
| } | ||
|
|
||
| fn show_all_rail_spaces(&mut self, network: Network, window: &mut Window, cx: &mut Context<Self>) { | ||
| self.close_rail_flyout(window, cx); | ||
| self.visit(Mode::Network(network), window, cx); | ||
| } | ||
|
Comment on lines
+331
to
+334
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: "N more" leads to the full list only on Discord. |
||
|
|
||
| /// A rail click: the place opens in the sidebar. In a window too narrow to keep the | ||
| /// sidebar in the row, it floats over the conversation. | ||
| pub(super) fn visit(&mut self, mode: Mode, window: &mut Window, cx: &mut Context<Self>) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| python3 "$(dirname "$0")/drive.py" linux |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| python3 "$(dirname "$0")/drive.py" macos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Major: Up and Down leave the flyout at the ends.
window.focus_nextandfocus_prevwalk every tab stop of the window and wrap at both ends (TabStopMap, pinned by gpui'stest_tab_handles). The flyout is painted last.Up right after opening, while the list itself has the focus, lands on the last stop before the flyout. Down on the last row wraps to the first stop of the window. Either way the focus is outside, and the lapse closes the flyout without returning focus to the opener.
The other menus wrap through their rows (
menu_step,overlay.rs:75).Smallest fix: keep an index as they do, or hold a handle per row and, after the step, move to the last or first row when the focus is no longer on one.
Not seen running: this follows from the focus code and that test.