wip: refactor

This commit is contained in:
2024-12-31 09:34:33 +07:00
parent 5b78e6ad12
commit b2f2491889
14 changed files with 205 additions and 120 deletions

View File

@@ -8,7 +8,7 @@ use crate::{
indicator::Indicator,
theme::{ActiveTheme, Colorize as _},
tooltip::Tooltip,
Disableable, Icon, Selectable, Sizable, Size,
Disableable, Icon, Selectable, Sizable, Size, StyledExt,
};
pub enum ButtonRounded {
@@ -169,6 +169,8 @@ pub struct Button {
size: Size,
compact: bool,
reverse: bool,
bold: bool,
centered: bool,
tooltip: Option<SharedString>,
on_click: OnClick,
pub(crate) stop_propagation: bool,
@@ -202,6 +204,8 @@ impl Button {
loading: false,
reverse: false,
compact: false,
bold: false,
centered: true,
children: Vec::new(),
loading_icon: None,
}
@@ -261,6 +265,16 @@ impl Button {
self
}
pub fn not_centered(mut self) -> Self {
self.centered = false;
self
}
pub fn bold(mut self) -> Self {
self.bold = true;
self
}
pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
self.on_click = Some(Box::new(handler));
self
@@ -340,7 +354,7 @@ impl RenderOnce for Button {
.id(self.id)
.flex()
.items_center()
.justify_center()
.when(self.centered, |this| this.justify_center())
.cursor_pointer()
.overflow_hidden()
.when(cx.theme().shadow && normal_style.shadow, |this| {
@@ -359,8 +373,8 @@ impl RenderOnce for Button {
// Normal Button
match self.size {
Size::Size(size) => this.px(size * 0.2),
Size::XSmall => this.h_5().px_2().when(self.compact, |this| this.px_0()),
Size::Small => this.h_6().px_3().when(self.compact, |this| this.px_0p5()),
Size::XSmall => this.h_5().px_1().when(self.compact, |this| this.px_0()),
Size::Small => this.h_6().px_2().when(self.compact, |this| this.px_0p5()),
_ => this.h_8().px_4().when(self.compact, |this| this.px_1()),
}
}
@@ -445,7 +459,7 @@ impl RenderOnce for Button {
.justify_center()
.map(|this| match self.size {
Size::XSmall => this.gap_1().text_xs(),
Size::Small => this.gap_1().text_xs(),
Size::Small => this.gap_2().text_xs(),
_ => this.gap_2().text_base(),
})
.when(!self.loading, |this| {
@@ -461,7 +475,13 @@ impl RenderOnce for Button {
)
})
.when_some(self.label, |this, label| {
this.child(div().flex_none().line_height(relative(1.)).child(label))
this.child(
div()
.flex_none()
.line_height(relative(1.))
.child(label)
.when(self.bold, |this| this.font_semibold()),
)
})
.children(self.children)
})

View File

@@ -40,6 +40,7 @@ pub enum IconName {
GalleryVerticalEnd,
GitHub,
Globe,
Group,
Heart,
HeartOff,
Inbox,
@@ -120,6 +121,7 @@ impl IconName {
Self::GalleryVerticalEnd => "icons/gallery-vertical-end.svg",
Self::GitHub => "icons/github.svg",
Self::Globe => "icons/globe.svg",
Self::Group => "icons/group.svg",
Self::Heart => "icons/heart.svg",
Self::HeartOff => "icons/heart-off.svg",
Self::Inbox => "icons/inbox.svg",

View File

@@ -11,6 +11,8 @@ use crate::{
actions!(menu, [Confirm, Dismiss, SelectNext, SelectPrev]);
const ITEM_HEIGHT: Pixels = px(26.);
pub fn init(cx: &mut AppContext) {
let context = Some("PopupMenu");
cx.bind_keys([
@@ -492,8 +494,6 @@ impl Render for PopupMenu {
let window_haft_height = cx.window_bounds().get_bounds().size.height * 0.5;
let max_height = window_haft_height.min(px(450.));
const ITEM_HEIGHT: Pixels = px(26.);
v_flex()
.id("popup-menu")
.key_context("PopupMenu")
@@ -540,11 +540,11 @@ impl Render for PopupMenu {
.map(|(ix, item)| {
let this = ListItem::new(("menu-item", ix))
.relative()
.text_sm()
.items_center()
.py_0()
.px_2()
.rounded_md()
.items_center()
.text_xs()
.on_mouse_enter(cx.listener(move |this, _, cx| {
this.hovered_menu_ix = Some(ix);
cx.notify();
@@ -667,7 +667,7 @@ impl Render for PopupMenu {
if hovered_ix == ix {
this.child(
anchored()
anchored()
.anchor(anchor)
.child(
div()
@@ -741,33 +741,3 @@ pub fn key_shortcut(key: Keystroke) -> String {
parts.push(&key);
parts.join("+")
}
#[cfg(test)]
mod tests {
#[test]
fn test_key_shortcut() {
use super::key_shortcut;
use gpui::Keystroke;
if cfg!(target_os = "windows") {
assert_eq!(key_shortcut(Keystroke::parse("a").unwrap()), "A");
assert_eq!(key_shortcut(Keystroke::parse("ctrl-a").unwrap()), "Ctrl+A");
assert_eq!(
key_shortcut(Keystroke::parse("ctrl-alt-a").unwrap()),
"Ctrl+Alt+A"
);
assert_eq!(
key_shortcut(Keystroke::parse("ctrl-alt-shift-a").unwrap()),
"Ctrl+Alt+Shift+A"
);
assert_eq!(
key_shortcut(Keystroke::parse("ctrl-alt-shift-win-a").unwrap()),
"Ctrl+Alt+Win+Shift+A"
);
assert_eq!(
key_shortcut(Keystroke::parse("ctrl-shift-backspace").unwrap()),
"Ctrl+Shift+Backspace"
);
}
}
}

View File

@@ -130,7 +130,7 @@ pub trait StyledExt: Styled + Sized {
.border_1()
.border_color(cx.theme().border)
.shadow_lg()
.rounded(px(cx.theme().radius))
.rounded_lg()
}
}