Skip to content

Commit 8f63aab

Browse files
CopilotGZTimeWalker
andcommitted
Add interactive navigation to sidebar with click handlers
Co-authored-by: GZTimeWalker <28180262+GZTimeWalker@users.noreply.github.com>
1 parent 6431d99 commit 8f63aab

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

crates/wsrx-desktop-gpui/src/views/root.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Root view - Main application window
2-
use gpui::{Context, Render, Window, div, prelude::*, Entity};
2+
use gpui::{Context, Render, Window, div, prelude::*, Entity, SharedString, WeakEntity};
33
use crate::models::Page;
44
use crate::styles::colors;
55
use super::{SidebarView, GetStartedView, ConnectionsView, NetworkLogsView, SettingsView};
@@ -22,18 +22,38 @@ impl RootView {
2222
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
2323
let current_page = Page::GetStarted;
2424

25-
Self {
25+
let mut root = Self {
2626
current_page,
2727
sidebar: cx.new(|cx| SidebarView::new(window, cx, current_page)),
2828
get_started: cx.new(|cx| GetStartedView::new(window, cx)),
2929
connections: cx.new(|cx| ConnectionsView::new(window, cx)),
3030
network_logs: cx.new(|cx| NetworkLogsView::new(window, cx)),
3131
settings: cx.new(|cx| SettingsView::new(window, cx)),
32-
}
32+
};
33+
34+
// Set up the navigation callback for sidebar
35+
let weak_self = cx.weak_entity();
36+
root.sidebar.update(cx, |sidebar, _| {
37+
sidebar.set_on_page_change(Box::new(move |page, cx| {
38+
if let Some(root) = weak_self.upgrade() {
39+
root.update(cx, |root, cx| {
40+
root.set_page(page, cx);
41+
});
42+
}
43+
}));
44+
});
45+
46+
root
3347
}
3448

35-
pub fn set_page(&mut self, page: Page, _cx: &mut Context<Self>) {
49+
pub fn set_page(&mut self, page: Page, cx: &mut Context<Self>) {
3650
self.current_page = page;
51+
// Update sidebar to reflect the new active page
52+
self.sidebar.update(cx, |sidebar, cx| {
53+
sidebar.set_active_page(page);
54+
cx.notify();
55+
});
56+
cx.notify(); // Trigger re-render
3757
}
3858

3959
fn render_sidebar(&self) -> impl IntoElement {
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,69 @@
11
// Sidebar view - Navigation sidebar
2-
use gpui::{Context, Render, Window, div, prelude::*};
2+
use gpui::{Context, Render, Window, div, prelude::*, App, SharedString};
33
use crate::models::Page;
44
use crate::styles::colors;
55

6+
type PageChangeCallback = Box<dyn Fn(Page, &mut App) + Send + Sync>;
7+
68
pub struct SidebarView {
79
active_page: Page,
10+
on_page_change: Option<PageChangeCallback>,
811
}
912

1013
impl SidebarView {
1114
pub fn new(_window: &mut Window, _cx: &mut Context<Self>, active_page: Page) -> Self {
12-
Self { active_page }
15+
Self {
16+
active_page,
17+
on_page_change: None,
18+
}
19+
}
20+
21+
pub fn set_on_page_change(&mut self, callback: PageChangeCallback) {
22+
self.on_page_change = Some(callback);
23+
}
24+
25+
pub fn set_active_page(&mut self, page: Page) {
26+
self.active_page = page;
1327
}
1428

15-
fn render_tab(&self, page: Page, label: impl Into<String>) -> impl IntoElement {
29+
fn render_tab(&self, page: Page, label: impl Into<String>, cx: &mut Context<Self>) -> impl IntoElement {
1630
let is_active = self.active_page == page;
1731
let label_text = label.into();
32+
let id = SharedString::from(format!("sidebar-tab-{:?}", page));
33+
1834
div()
35+
.id(id)
1936
.flex()
2037
.items_center()
2138
.px_4()
2239
.py_3()
40+
.cursor_pointer()
2341
.when(is_active, |div| {
2442
div.bg(colors::accent())
2543
})
2644
.when(!is_active, |div| {
2745
div.hover(|div| div.bg(gpui::rgba(0x00000030)))
2846
})
47+
.on_click(cx.listener(move |this, _event, _window, cx| {
48+
if let Some(ref callback) = this.on_page_change {
49+
callback(page, cx);
50+
}
51+
}))
2952
.child(label_text)
3053
}
3154
}
3255

3356
impl Render for SidebarView {
34-
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
57+
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
3558
div()
3659
.flex()
3760
.flex_col()
3861
.gap_1()
3962
.p_2()
40-
.child(self.render_tab(Page::GetStarted, "Get Started"))
41-
.child(self.render_tab(Page::Connections, "Connections"))
42-
.child(self.render_tab(Page::NetworkLogs, "Network Logs"))
43-
.child(self.render_tab(Page::Settings, "Settings"))
63+
.child(self.render_tab(Page::GetStarted, "Get Started", cx))
64+
.child(self.render_tab(Page::Connections, "Connections", cx))
65+
.child(self.render_tab(Page::NetworkLogs, "Network Logs", cx))
66+
.child(self.render_tab(Page::Settings, "Settings", cx))
4467
}
4568
}
4669

0 commit comments

Comments
 (0)