Making it easier to draw focus correctly

This commit is contained in:
Jonathan Johnson 2023-11-29 15:08:12 -08:00
parent 0684ac59bf
commit ce6935a7a7
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
5 changed files with 8 additions and 7 deletions

View file

@ -20,7 +20,7 @@ fn main() -> gooey::Result {
.on_accept_focus(|context| context.enabled()) .on_accept_focus(|context| context.enabled())
.on_redraw(|context| { .on_redraw(|context| {
context.fill(context.theme().secondary.color); context.fill(context.theme().secondary.color);
if context.focused() { if context.focused(true) {
context.draw_focus_ring(); context.draw_focus_ring();
} }
}) })

View file

@ -979,7 +979,7 @@ impl<'context, 'window> WidgetContext<'context, 'window> {
/// Widget events relating to focus changes are deferred until after the all /// Widget events relating to focus changes are deferred until after the all
/// contexts for the currently firing event are dropped. /// contexts for the currently firing event are dropped.
pub fn blur(&mut self) -> bool { pub fn blur(&mut self) -> bool {
if self.focused() { if self.focused(true) {
self.clear_focus(); self.clear_focus();
true true
} else { } else {
@ -1052,8 +1052,9 @@ impl<'context, 'window> WidgetContext<'context, 'window> {
/// Returns true if this widget is currently focused for user input. /// Returns true if this widget is currently focused for user input.
#[must_use] #[must_use]
pub fn focused(&self) -> bool { pub fn focused(&self, check_window: bool) -> bool {
self.pending_state.focus == Some(self.current_node.id()) self.pending_state.focus == Some(self.current_node.id())
&& (!check_window || self.window.focused().get_tracking_refresh(self))
} }
/// Returns true if this widget is the target to activate when the user /// Returns true if this widget is the target to activate when the user

View file

@ -343,7 +343,7 @@ impl Widget for Button {
let two_lp_stroke = StrokeOptions::lp_wide(Lp::points(2)); let two_lp_stroke = StrokeOptions::lp_wide(Lp::points(2));
context.stroke_outline(style.outline, two_lp_stroke); context.stroke_outline(style.outline, two_lp_stroke);
if context.focused() { if context.focused(true) {
if current_style == ButtonKind::Transparent { if current_style == ButtonKind::Transparent {
let two_lp_stroke = two_lp_stroke.into_px(context.gfx.scale()); let two_lp_stroke = two_lp_stroke.into_px(context.gfx.scale());
let focus_color = context.get(&HighlightColor); let focus_color = context.get(&HighlightColor);

View file

@ -1023,7 +1023,7 @@ where
let cache = self.layout_text(Some(size.width.into_signed()), context); let cache = self.layout_text(Some(size.width.into_signed()), context);
let highlight = if context.focused() && window_focused { let highlight = if context.focused(false) && window_focused {
context.draw_focus_ring(); context.draw_focus_ring();
context.get(&HighlightColor) context.get(&HighlightColor)
} else { } else {
@ -1032,7 +1032,7 @@ where
outline_color outline_color
}; };
if context.focused() { if context.focused(false) {
context.set_ime_allowed(true); context.set_ime_allowed(true);
context.set_ime_purpose(if cache.masked { context.set_ime_purpose(if cache.masked {
ImePurpose::Password ImePurpose::Password

View file

@ -219,7 +219,7 @@ where
// Draw the knob // Draw the knob
if spec.knob_size > 0 { if spec.knob_size > 0 {
let focus = context.focused().then_some(self.focused_knob).flatten(); let focus = context.focused(true).then_some(self.focused_knob).flatten();
Self::draw_knobs( Self::draw_knobs(
self.flip_pt_if_vertical(Point::new(end + spec.half_knob, spec.half_knob) + inset), self.flip_pt_if_vertical(Point::new(end + spec.half_knob, spec.half_knob) + inset),
spec.end.map(|_| { spec.end.map(|_| {