mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
fix(desktop): show borders on property name fields
This commit is contained in:
1 parent
cbb91937eb
commit
08ee3cb22a
2 files changed
+180
-11
No files matched your search
+36
-8
@@ -25,6 +25,7 @@ pub(super) struct PropertyInput {
|
||||
commit: Option<Commit>,
|
||||
blur: Option<Subscription>,
|
||||
palette: DesktopPalette,
|
||||
framed: bool,
|
||||
bounds: Option<Bounds<Pixels>>,
|
||||
multiline: bool,
|
||||
area: Option<TextAreaLayout>,
|
||||
@@ -79,6 +80,7 @@ impl PropertyInput {
|
||||
placeholder,
|
||||
snapshot,
|
||||
palette,
|
||||
framed: false,
|
||||
commit,
|
||||
blur: None,
|
||||
bounds: None,
|
||||
@@ -99,6 +101,11 @@ impl PropertyInput {
|
||||
self.focus.clone()
|
||||
}
|
||||
|
||||
pub(super) fn frame_for_popover(&mut self, palette: DesktopPalette) {
|
||||
self.palette = palette;
|
||||
self.framed = true;
|
||||
}
|
||||
|
||||
pub(super) fn text(&self) -> String {
|
||||
self.buffer.clone().into_committed_text()
|
||||
}
|
||||
@@ -408,24 +415,18 @@ impl Render for PropertyInput {
|
||||
} else {
|
||||
self.single_line_field(focused, cx).into_any_element()
|
||||
};
|
||||
div()
|
||||
let field = div()
|
||||
.id(ui::SharedString::from(self.snapshot.clone()))
|
||||
.snapshot(ui::snap_id!("{}", self.snapshot), None)
|
||||
.snapshot_focused(focused)
|
||||
.track_focus(&self.focus.handle)
|
||||
.border_1()
|
||||
.border_color(if focused {
|
||||
palette.accent
|
||||
} else {
|
||||
ui::transparent_black()
|
||||
})
|
||||
.min_w_0()
|
||||
.w_full()
|
||||
.child(self.focus.measure())
|
||||
.relative()
|
||||
.min_h(px(30.0))
|
||||
.rounded(px(4.0))
|
||||
.hover(move |style| style.bg(palette.hover_color))
|
||||
.cursor(ui::CursorStyle::IBeam)
|
||||
.child(text)
|
||||
.overflow_hidden()
|
||||
@@ -444,7 +445,34 @@ impl Render for PropertyInput {
|
||||
window.request_virtual_keyboard();
|
||||
cx.stop_propagation();
|
||||
}),
|
||||
)
|
||||
);
|
||||
if self.framed {
|
||||
let theme = if palette.bg_canvas.l < 0.5 {
|
||||
ui_tokens::ThemeMode::Dark
|
||||
} else {
|
||||
ui_tokens::ThemeMode::Light
|
||||
};
|
||||
let tokens = crate::theme_tokens_for(theme);
|
||||
let background = crate::theme_color(
|
||||
tokens,
|
||||
"--affine-v2-input-background",
|
||||
tokens.background_primary,
|
||||
);
|
||||
let border = if focused {
|
||||
palette.accent
|
||||
} else {
|
||||
crate::theme_color(tokens, "--affine-v2-input-border-default", tokens.border)
|
||||
};
|
||||
field.bg(background).border_color(border)
|
||||
} else {
|
||||
field
|
||||
.border_color(if focused {
|
||||
palette.accent
|
||||
} else {
|
||||
ui::transparent_black()
|
||||
})
|
||||
.hover(move |style| style.bg(palette.hover_color))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+144
-3
@@ -1,6 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use ui::{App, Div, InteractiveElement, ParentElement, Styled, div, px};
|
||||
use ui::{App, Div, Entity, InteractiveElement, ParentElement, Styled, div, px};
|
||||
use ui_tokens::{
|
||||
ThemeMode,
|
||||
icons::{self, IconDef},
|
||||
@@ -8,6 +8,7 @@ use ui_tokens::{
|
||||
|
||||
use super::{
|
||||
custom_rows::{KINDS, Menu, Panel, Row, activate, builtin_icon, close_menu, icon},
|
||||
property_input::PropertyInput,
|
||||
row_order::OrderedRow,
|
||||
};
|
||||
|
||||
@@ -22,7 +23,7 @@ impl Panel<'_> {
|
||||
let input = self.input("create.name", "", "Property name", None, false);
|
||||
let mut menu = self
|
||||
.menu("create")
|
||||
.child(input.clone())
|
||||
.child(self.name_field(input.clone()))
|
||||
.child(self.menu_label("Type"));
|
||||
for (kind, label, glyph) in KINDS {
|
||||
let (button, stop) = self.control(&format!("create.{kind}"), label, Some(glyph));
|
||||
@@ -83,7 +84,7 @@ impl Panel<'_> {
|
||||
);
|
||||
let mut menu = self
|
||||
.menu("edit")
|
||||
.child(field)
|
||||
.child(self.name_field(field))
|
||||
.child(self.menu_label(&format!("Type {}", row.kind)))
|
||||
.child(self.menu_label("Visibility"));
|
||||
let palette = self.properties.palette;
|
||||
@@ -172,6 +173,13 @@ impl Panel<'_> {
|
||||
}))
|
||||
}
|
||||
|
||||
fn name_field(&mut self, input: Entity<PropertyInput>) -> Div {
|
||||
input.update(self.app, |field, _| {
|
||||
field.frame_for_popover(self.properties.palette);
|
||||
});
|
||||
div().mx(px(4.0)).mt(px(4.0)).mb(px(2.0)).child(input)
|
||||
}
|
||||
|
||||
fn edit(
|
||||
&self,
|
||||
id: &str,
|
||||
@@ -197,3 +205,136 @@ impl Panel<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ui::{
|
||||
AnyWindowHandle, AppContext, Application, Bounds, HeadlessPlatform, Keystroke,
|
||||
MouseMoveEvent, PlatformInput, WindowBounds, WindowOptions, point, size,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
use crate::{DesktopAppState, DesktopShellView, DocInspectorTab, register_desktop_fonts};
|
||||
|
||||
#[test]
|
||||
fn property_popovers_draw_name_field_borders_in_both_focus_states() {
|
||||
const DOC: &str = "doc-sync";
|
||||
const PREFIX: &str = "desktop.inspector.custom";
|
||||
|
||||
for theme in [ThemeMode::Light, ThemeMode::Dark] {
|
||||
let mut state = DesktopAppState::seed_showcase(theme, "2026-12-02");
|
||||
state.open_doc_in_active_pane(DOC);
|
||||
state.inspector_tab = Some(DocInspectorTab::Info);
|
||||
let id = state
|
||||
.store
|
||||
.create_custom_property(DOC, "Priority", "text")
|
||||
.expect("create property");
|
||||
let platform = HeadlessPlatform::new(1280, 800, 1.0);
|
||||
let app = Application::with_platform(platform.clone()).run_embedded(|_| {});
|
||||
let handle = app.update(|cx| {
|
||||
register_desktop_fonts(cx).expect("desktop fonts");
|
||||
cx.open_window(
|
||||
WindowOptions {
|
||||
window_bounds: Some(WindowBounds::Windowed(Bounds::new(
|
||||
point(px(0.0), px(0.0)),
|
||||
size(px(1280.0), px(800.0)),
|
||||
))),
|
||||
..WindowOptions::default()
|
||||
},
|
||||
|_, cx| cx.new(|cx| DesktopShellView::new(state, cx)),
|
||||
)
|
||||
.expect("desktop window")
|
||||
});
|
||||
let platform_window = platform.windows().pop().expect("headless window");
|
||||
platform_window.request_frame();
|
||||
|
||||
let click = |id: &str| {
|
||||
let bounds = app.update(|cx| {
|
||||
handle
|
||||
.update(cx, |_, window, _| {
|
||||
window.frame_snapshot().elements[id].bounds
|
||||
})
|
||||
.expect("desktop view")
|
||||
});
|
||||
platform_window.simulate_click(point(
|
||||
px(bounds.x + bounds.width / 2.0),
|
||||
px(bounds.y + bounds.height / 2.0),
|
||||
));
|
||||
platform_window.dispatch_input(PlatformInput::MouseMove(MouseMoveEvent {
|
||||
position: point(px(0.0), px(0.0)),
|
||||
..Default::default()
|
||||
}));
|
||||
platform_window.request_frame();
|
||||
};
|
||||
for (menu, anchor, name) in [
|
||||
("create", "add".to_owned(), "create.name".to_owned()),
|
||||
("edit", format!("{id}.name"), format!("{id}.edit.name")),
|
||||
] {
|
||||
let anchor = format!("{PREFIX}.{anchor}");
|
||||
let input_id = format!("{PREFIX}.{name}");
|
||||
let menu_id = format!("{PREFIX}.menu.{menu}");
|
||||
click(&anchor);
|
||||
let capture = |focused| {
|
||||
app.update(|cx| {
|
||||
handle
|
||||
.update(cx, |_, window, _| {
|
||||
let frame = window.render_to_image().expect("offscreen frame");
|
||||
let snapshot = window.frame_snapshot();
|
||||
let input = &snapshot.elements[&input_id];
|
||||
assert_eq!(input.focused, focused, "{theme:?} {menu}");
|
||||
(frame, input.bounds, snapshot.elements[&menu_id].bounds)
|
||||
})
|
||||
.expect("desktop view")
|
||||
})
|
||||
};
|
||||
let (resting, bounds, menu_bounds) = capture(false);
|
||||
click(&input_id);
|
||||
let (focused, focused_bounds, _) = capture(true);
|
||||
app.update(|cx| {
|
||||
AnyWindowHandle::from(handle)
|
||||
.update(cx, |_, window, cx| {
|
||||
let escape = Keystroke::parse("escape").expect("escape key");
|
||||
window.dispatch_keystroke(escape, cx);
|
||||
})
|
||||
.expect("desktop view");
|
||||
});
|
||||
platform_window.request_frame();
|
||||
|
||||
assert_eq!(bounds, focused_bounds, "{theme:?} {menu}");
|
||||
assert_eq!(bounds.height, 30.0, "{theme:?} {menu}");
|
||||
assert_eq!(bounds.x - menu_bounds.x, 9.0, "{theme:?} {menu}");
|
||||
assert_eq!(
|
||||
menu_bounds.right() - bounds.right(),
|
||||
9.0,
|
||||
"{theme:?} {menu}"
|
||||
);
|
||||
assert_eq!(bounds.y - menu_bounds.y, 9.0, "{theme:?} {menu}");
|
||||
|
||||
let x = bounds.x as u32;
|
||||
let y = bounds.y as u32;
|
||||
let w = bounds.width as u32;
|
||||
let h = bounds.height as u32;
|
||||
let accent = ui::Rgba::from(crate::palette_for(theme).accent);
|
||||
let accent = [accent.r, accent.g, accent.b].map(|channel| (channel * 255.0) as u8);
|
||||
let fill = resting.get_pixel(x + w / 2, y + h / 2);
|
||||
for (x, y) in [
|
||||
(x + w / 2, y),
|
||||
(x + w / 2, y + h - 1),
|
||||
(x, y + h / 2),
|
||||
(x + w - 1, y + h / 2),
|
||||
] {
|
||||
let border = resting.get_pixel(x, y);
|
||||
assert_ne!(border, fill, "{theme:?} {menu} border at {x}, {y}");
|
||||
let painted = focused.get_pixel(x, y);
|
||||
assert!(
|
||||
painted.0[..3]
|
||||
.iter()
|
||||
.zip(accent)
|
||||
.all(|(actual, expected)| actual.abs_diff(expected) <= 3),
|
||||
"{theme:?} {menu} focus border at {x}, {y}: {painted:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user