Skip to content

Commit

Permalink
feat: Sidebar component (marc2332#513)
Browse files Browse the repository at this point in the history
* feat: `Sidebar` as a component in freya-components

* theming

* clean up

* Update password.rs

* do it

* Update sidebar.rs

* Revert "Merge branch 'main' of https://github.com/tigerros/freya"

This reverts commit e560b55, reversing
changes made to 84899b6.

* feat: `Sidebar` as a component in freya-components

* theming

* clean up

* do it

* Update password.rs

* rebase

* add

* finish

* Update sidebar.rs

* :godmode:

* pub mod

* clean up

* fixes and cleanup

---------

Co-authored-by: marc2332 <[email protected]>
  • Loading branch information
tigerros and marc2332 authored Feb 20, 2024
1 parent 84e108d commit fda8624
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 173 deletions.
2 changes: 2 additions & 0 deletions crates/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod loader;
mod network_image;
mod progress_bar;
mod scroll_views;
mod sidebar;
mod slider;
mod switch;
mod table;
Expand All @@ -39,6 +40,7 @@ pub use loader::*;
pub use network_image::*;
pub use progress_bar::*;
pub use scroll_views::*;
pub use sidebar::*;
pub use slider::*;
pub use switch::*;
pub use table::*;
Expand Down
124 changes: 124 additions & 0 deletions crates/components/src/sidebar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

use crate::{ButtonStatus, ScrollView};
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_hooks::{
theme_with, use_applied_theme, use_platform, ScrollViewThemeWith, SidebarItemTheme,
SidebarItemThemeWith, SidebarTheme, SidebarThemeWith,
};
use winit::window::CursorIcon;

#[allow(non_snake_case)]
#[component]
pub fn Sidebar(
/// Theme override.
theme: Option<SidebarThemeWith>,
/// This is what is rendered next to the sidebar.
children: Element,
/// This is what is rendered in the sidebar.
sidebar: Element,
) -> Element {
let SidebarTheme {
font_theme,
background,
} = use_applied_theme!(&theme, sidebar);

rsx!(
rect {
width: "100%",
height: "100%",
direction: "horizontal",
rect {
overflow: "clip",
width: "170",
height: "100%",
background: "{background}",
color: "{font_theme.color}",
shadow: "2 0 5 0 rgb(0, 0, 0, 30)",
ScrollView {
theme: theme_with!(ScrollViewTheme {
padding: "16".into(),
}),
{sidebar}
}
}
rect {
overflow: "clip",
width: "fill",
height: "100%",
color: "{font_theme.color}",
{children}
}
}
)
}

#[allow(non_snake_case)]
#[component]
pub fn SidebarItem(
/// Theme override.
theme: Option<SidebarItemThemeWith>,
/// Inner content for the SidebarItem.
children: Element,
/// Optionally handle the `onclick` event in the SidebarItem.
onclick: Option<EventHandler<()>>,
) -> Element {
let SidebarItemTheme {
hover_background,
background,
font_theme,
border_fill,
} = use_applied_theme!(&theme, sidebar_item);
let mut status = use_signal(ButtonStatus::default);
let platform = use_platform();

use_drop(move || {
if *status.read() == ButtonStatus::Hovering {
platform.set_cursor(CursorIcon::default());
}
});

let onclick = move |_| {
if let Some(onclick) = &onclick {
onclick.call(());
}
};

let onmouseenter = move |_| {
platform.set_cursor(CursorIcon::Pointer);
status.set(ButtonStatus::Hovering);
};

let onmouseleave = move |_| {
platform.set_cursor(CursorIcon::default());
status.set(ButtonStatus::default());
};

let background = match *status.read() {
ButtonStatus::Hovering => hover_background,
ButtonStatus::Idle => background,
};

rsx!(
rect {
overflow: "clip",
margin: "5 0",
onclick,
onmouseenter,
onmouseleave,
width: "100%",
height: "auto",
color: "{font_theme.color}",
border: "1 solid {border_fill}",
shadow: "0 4 5 0 rgb(0, 0, 0, 30)",
corner_radius: "8",
padding: "8",
background: "{background}",
{children}
}
)
}
11 changes: 9 additions & 2 deletions crates/core/src/events/event_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,18 @@ impl EventName {

// Bubble all events except:
// - Keyboard events
// - Mouseleave events
// - Mouse movements events
pub fn does_bubble(&self) -> bool {
!matches!(
self,
Self::KeyDown | Self::KeyUp | Self::MouseLeave | Self::PointerLeave
Self::KeyDown
| Self::KeyUp
| Self::MouseLeave
| Self::PointerLeave
| Self::MouseEnter
| Self::PointerEnter
| Self::MouseOver
| Self::PointerOver
)
}

Expand Down
14 changes: 14 additions & 0 deletions crates/hooks/src/theming/dark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,18 @@ pub const DARK_THEME: Theme = Theme {
height: LIGHT_THEME.arrow_icon.height,
margin: LIGHT_THEME.arrow_icon.margin,
},
sidebar: SidebarTheme {
background: cow_borrowed!("rgb(20, 20, 20)"),
font_theme: FontTheme {
color: cow_borrowed!("white"),
},
},
sidebar_item: SidebarItemTheme {
background: cow_borrowed!("rgb(35, 35, 35)"),
hover_background: cow_borrowed!("rgb(45, 45, 45)"),
border_fill: cow_borrowed!("rgb(80, 80, 80)"),
font_theme: FontTheme {
color: cow_borrowed!("white"),
},
},
};
14 changes: 14 additions & 0 deletions crates/hooks/src/theming/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,18 @@ pub const LIGHT_THEME: Theme = Theme {
height: cow_borrowed!("10"),
margin: cow_borrowed!("none"),
},
sidebar: SidebarTheme {
background: cow_borrowed!("rgb(245, 245, 245)"),
font_theme: FontTheme {
color: cow_borrowed!("rgb(10, 10, 10)"),
},
},
sidebar_item: SidebarItemTheme {
background: cow_borrowed!("rgb(245, 245, 245)"),
hover_background: cow_borrowed!("rgb(235, 235, 235)"),
font_theme: FontTheme {
color: cow_borrowed!("rgb(10, 10, 10)"),
},
border_fill: cow_borrowed!("rgb(210, 210, 210)"),
},
};
24 changes: 24 additions & 0 deletions crates/hooks/src/theming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,28 @@ define_theme! {
}
}

define_theme! {
%[component]
pub Sidebar {
%[cows]
background: str,
%[subthemes]
font_theme: FontTheme,
}
}

define_theme! {
%[component]
pub SidebarItem {
%[cows]
background: str,
hover_background: str,
border_fill: str,
%[subthemes]
font_theme: FontTheme,
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Theme {
pub name: &'static str,
Expand All @@ -470,6 +492,8 @@ pub struct Theme {
pub graph: GraphTheme,
pub network_image: NetworkImageTheme,
pub arrow_icon: ArrowIconTheme,
pub sidebar: SidebarTheme,
pub sidebar_item: SidebarItemTheme,
}

impl Default for Theme {
Expand Down
99 changes: 99 additions & 0 deletions examples/router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]

use dioxus_router::prelude::{Outlet, Routable, Router};
use freya::prelude::*;

fn main() {
launch_with_props(app, "Router Example", (550.0, 400.0));
}

fn app() -> Element {
rsx!(Router::<Route> {})
}

#[derive(Routable, Clone)]
#[rustfmt::skip]
pub enum Route {
#[layout(AppSidebar)]
#[route("/")]
Home,
#[route("/wow")]
Wow,
#[end_layout]
#[route("/..route")]
PageNotFound { },
}

#[allow(non_snake_case)]
fn AppSidebar() -> Element {
rsx!(
Sidebar {
sidebar: rsx!(
Link {
to: Route::Home,
SidebarItem {
label {
"Go to Hey ! 👋"
}
},
},
Link {
to: Route::Wow,
SidebarItem {
label {
"Go to Wow! 👈"
}
},
},
SidebarItem {
onclick: |_| println!("Hello!"),
label {
"Print Hello! 👀"
}
},
),
Body {
rect {
main_align: "center",
cross_align: "center",
width: "100%",
height: "100%",
Outlet::<Route> { }
}
}
}
)
}

#[allow(non_snake_case)]
#[component]
fn Home() -> Element {
rsx!(
label {
"Just some text 😗 in /"
}
)
}

#[allow(non_snake_case)]
#[component]
fn Wow() -> Element {
rsx!(
label {
"Just more text 👈!! in /wow"
}
)
}

#[allow(non_snake_case)]
#[component]
fn PageNotFound() -> Element {
rsx!(
label {
"404!! 😵"
}
)
}
Loading

0 comments on commit fda8624

Please sign in to comment.