History

Thread click handler through from workspace to language selector

Marshall Bowers committed 3 years ago

Commit 95ef61b1 parent c1426764 files · +46 −18Browse files

Unified · Split

4 files
  1. crates/ui2/src/components/icon_button.rs +2 −2
  2. crates/ui2/src/components/status_bar.rs +5 −5
  3. crates/ui2/src/components/workspace.rs +33 −4
  4. crates/ui2/src/elements/button.rs +6 −7
crates/ui2/src/components/icon_button.rs+2−2
@@ -4,10 +4,10 @@ use std::sync::Arc; · 3 unmodified lines
44 use gpui3::{Interactive, MouseButton};
55
66 use crate::prelude::*;
7−use crate::{theme, Icon, IconColor, IconElement};
7+use crate::{theme, ClickHandler, Icon, IconColor, IconElement};
88
99 struct IconButtonHandlers<S: 'static + Send + Sync> {
10− click: Option<Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>>,
10+ click: Option<ClickHandler<S>>,
1111 }
1212
1313 impl<S: 'static + Send + Sync> Default for IconButtonHandlers<S> {
crates/ui2/src/components/status_bar.rs+5−5
@@ -1,7 +1,7 @@
11 use std::marker::PhantomData;
22
33 use crate::prelude::*;
4−use crate::{Button, Icon, IconButton, IconColor, ToolDivider};
4+use crate::{Button, ClickHandler, Icon, IconButton, IconColor, ToolDivider};
55
66 #[derive(Default, PartialEq)]
77 pub enum Tool {
@@ -34,15 +34,17 @@ pub struct StatusBar<S: 'static + Send + Sync + Clone> { · 26 unmodified lines
3434 left_tools: Option<ToolGroup>,
3535 right_tools: Option<ToolGroup>,
3636 bottom_tools: Option<ToolGroup>,
37+ on_select_language: ClickHandler<S>,
3738 }
3839
3940 impl<S: 'static + Send + Sync + Clone> StatusBar<S> {
40− pub fn new() -> Self {
41+ pub fn new(on_select_language: ClickHandler<S>) -> Self {
4142 Self {
4243 state_type: PhantomData,
4344 left_tools: None,
4445 right_tools: None,
4546 bottom_tools: None,
47+ on_select_language,
4648 }
4749 }
4850
@@ -119,9 +121,7 @@ impl<S: 'static + Send + Sync + Clone> StatusBar<S> { · 70 unmodified lines
119121 .items_center()
120122 .gap_1()
121123 .child(Button::new("116:25"))
122− .child(
123− Button::new("Rust").on_click(|_, _| println!("Select Language clicked.")),
124− ),
124+ .child(Button::new("Rust").on_click(self.on_select_language.clone())),
125125 )
126126 .child(ToolDivider::new())
127127 .child(
crates/ui2/src/components/workspace.rs+33−4
@@ -1,4 +1,5 @@
11 use std::marker::PhantomData;
2+use std::sync::atomic::{AtomicBool, Ordering};
23 use std::sync::Arc;
34
45 use chrono::DateTime;
@@ -7,9 +8,9 @@ use gpui3::{relative, rems, Size}; · 2 unmodified lines
78 use crate::prelude::*;
89 use crate::{
910 hello_world_rust_editor_with_status_example, random_players_with_call_status, theme, v_stack,
10− ChatMessage, ChatPanel, EditorPane, Label, Livestream, Pane, PaneGroup, Panel,
11− PanelAllowedSides, PanelSide, ProjectPanel, SplitDirection, StatusBar, Terminal, TitleBar,
12− Toast, ToastOrigin,
11+ ChatMessage, ChatPanel, EditorPane, Label, LanguageSelector, Livestream, Pane, PaneGroup,
12+ Panel, PanelAllowedSides, PanelSide, ProjectPanel, SplitDirection, StatusBar, Terminal,
13+ TitleBar, Toast, ToastOrigin,
1314 };
1415
1516 #[derive(Element)]
@@ -19,6 +20,7 @@ pub struct WorkspaceElement<S: 'static + Send + Sync + Clone> { · 3 unmodified lines
1920 right_panel_scroll_state: ScrollState,
2021 tab_bar_scroll_state: ScrollState,
2122 bottom_panel_scroll_state: ScrollState,
23+ show_language_selector: Arc<AtomicBool>,
2224 }
2325
2426 impl<S: 'static + Send + Sync + Clone> WorkspaceElement<S> {
@@ -29,12 +31,15 @@ impl<S: 'static + Send + Sync + Clone> WorkspaceElement<S> { · 4 unmodified lines
2931 right_panel_scroll_state: ScrollState::default(),
3032 tab_bar_scroll_state: ScrollState::default(),
3133 bottom_panel_scroll_state: ScrollState::default(),
34+ show_language_selector: Arc::new(AtomicBool::new(false)),
3235 }
3336 }
3437
3538 pub fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
3639 let theme = theme(cx).clone();
3740
41+ let show_language_selector = self.show_language_selector.clone();
42+
3843 let temp_size = rems(36.).into();
3944
4045 let root_group = PaneGroup::new_groups(
@@ -182,7 +187,31 @@ impl<S: 'static + Send + Sync + Clone> WorkspaceElement<S> { · 141 unmodified lines
182187 .side(PanelSide::Right),
183188 ),
184189 )
185− .child(StatusBar::new())
190+ .child(StatusBar::new(Arc::new(move |_, cx| {
191+ let is_showing_language_selector = show_language_selector.load(Ordering::SeqCst);
192+
193+ show_language_selector
194+ .compare_exchange(
195+ is_showing_language_selector,
196+ !is_showing_language_selector,
197+ Ordering::SeqCst,
198+ Ordering::SeqCst,
199+ )
200+ .unwrap();
201+
202+ cx.notify();
203+ })))
204+ .children(
205+ Some(
206+ div()
207+ .absolute()
208+ .top_0()
209+ .left_0()
210+ .z_index(999)
211+ .child(LanguageSelector::new()),
212+ )
213+ .filter(|_| self.show_language_selector.load(Ordering::SeqCst)),
214+ )
186215 .child(Toast::new(
187216 ToastOrigin::Bottom,
188217 |_, _| vec![Label::new("A toast").into_any()],
crates/ui2/src/elements/button.rs+6−7
@@ -20,8 +20,10 @@ pub enum ButtonVariant { · 19 unmodified lines
2020 Filled,
2121 }
2222
23+pub type ClickHandler<S> = Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>;
24+
2325 struct ButtonHandlers<S: 'static + Send + Sync> {
24− click: Option<Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>>,
26+ click: Option<ClickHandler<S>>,
2527 }
2628
2729 impl<S: 'static + Send + Sync> Default for ButtonHandlers<S> {
@@ -94,11 +96,8 @@ impl<S: 'static + Send + Sync + Clone> Button<S> { · 66 unmodified lines
9496 self
9597 }
9698
97− pub fn on_click(
98− mut self,
99− handler: impl Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync,
100− ) -> Self {
101− self.handlers.click = Some(Arc::new(handler));
99+ pub fn on_click(mut self, handler: ClickHandler<S>) -> Self {
100+ self.handlers.click = Some(handler);
102101 self
103102 }
104103
@@ -403,7 +402,7 @@ mod stories { · 298 unmodified lines
403402 .child(
404403 Button::new("Label")
405404 .variant(ButtonVariant::Ghost)
406− .on_click(|_view, _cx| println!("Button clicked.")),
405+ .on_click(Arc::new(|_view, _cx| println!("Button clicked."))),
407406 )
408407 }
409408 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t