Phase 1 R2: widget setters for Inspector live-apply
Widget trait gains default no-op setters set_password, set_show_password, set_cursor_visible, set_direction, set_page_increment, set_page_size, set_multi_select and set_items. EntryWidget: show_password + cursor_visible (mask only when password && !show_password; caret gated on cursor_visible). ButtonWidget: direction (0=Up 1=Down 2=Left 3=Right 4=None) with a MenuButton arrow triangle on the side the popup opens toward. RangeWidget: page_size (25.0) + page_increment (10.0) with PgUp/PgDn key handling. ListSelWidget: set_items (replace items, clamp selection) + set_multi_select override. draw.rs gains draw_triangle_left (mirror of draw_triangle_right).
This commit is contained in:
1 parent
e716ff4497
commit
cf6995fcc0
3 files changed
+124
-3
No files matched your search
@@ -265,3 +265,17 @@ pub fn draw_triangle_right(
|
||||
target.draw_line(cx + i, cy - (size - i), cx + i, cy + (size - i));
|
||||
}
|
||||
}
|
||||
|
||||
/// C++ `xf::draw_triangle_left` (mirror of `draw_triangle_right`).
|
||||
pub fn draw_triangle_left(
|
||||
target: &mut (impl DrawTarget + ?Sized),
|
||||
color: u32,
|
||||
cx: i32,
|
||||
cy: i32,
|
||||
size: i32,
|
||||
) {
|
||||
target.set_foreground(color);
|
||||
for i in 0..size {
|
||||
target.draw_line(cx - i, cy - (size - i), cx - i, cy + (size - i));
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
//! release / motion / key handlers, following the C++ semantics surveyed
|
||||
//! in `widgets/common/` (see `widget.rs` for the shared helpers).
|
||||
|
||||
use xframe_core::draw::{draw_checkmark, draw_polished, draw_text_at, draw_text_centered, draw_text_left, draw_triangle_down, text_width};
|
||||
use xframe_core::draw::{draw_checkmark, draw_polished, draw_text_at, draw_text_centered, draw_text_left, draw_triangle_down, draw_triangle_left, draw_triangle_right, draw_triangle_up, text_width};
|
||||
use xframe_core::{DrawTarget, Srgb, Style, StyleManager};
|
||||
|
||||
use crate::graph::XGraph;
|
||||
@@ -75,6 +75,9 @@ pub struct ButtonWidget {
|
||||
pub disabled_text_color: Srgb,
|
||||
/// Latched/on state (C++ `InstEmergencyStop` / `InstPushButton` `set_on`).
|
||||
pub on: bool,
|
||||
/// MenuButton popup direction (C++ `GtkMenuButton::direction_`,
|
||||
/// kArrowValues: 0=Up 1=Down 2=Left 3=Right 4=None; default Down).
|
||||
pub direction: i32,
|
||||
}
|
||||
|
||||
impl ButtonWidget {
|
||||
@@ -86,6 +89,7 @@ impl ButtonWidget {
|
||||
on_click_cb: None,
|
||||
disabled_text_color: Srgb::new(140, 140, 140),
|
||||
on: false,
|
||||
direction: 1,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,18 +107,40 @@ impl ButtonWidget {
|
||||
pub fn set_on(&mut self, on: bool) {
|
||||
self.on = on;
|
||||
}
|
||||
/// MenuButton direction (0=Up 1=Down 2=Left 3=Right 4=None).
|
||||
pub fn get_direction(&self) -> i32 {
|
||||
self.direction
|
||||
}
|
||||
pub fn set_direction(&mut self, d: i32) {
|
||||
self.direction = d.clamp(0, 4);
|
||||
}
|
||||
pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> ButtonWidget {
|
||||
ButtonWidget::new(name, kind, x, y, w, h)
|
||||
}
|
||||
}
|
||||
|
||||
archetype_impl!(ButtonWidget, Archetype::Button,
|
||||
fn set_direction(&mut self, d: i32) {
|
||||
self.direction = d.clamp(0, 4);
|
||||
}
|
||||
fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) {
|
||||
if !self.state.visible {
|
||||
return;
|
||||
}
|
||||
let style = resolve_style(styles, &self.state.style_name);
|
||||
draw_button_body(target, &style, &self.state, self.disabled_text_color.to_pixel());
|
||||
// MenuButton: arrow triangle on the side the popup opens toward
|
||||
// (Left on the left, otherwise on the right; None hides it).
|
||||
if self.kind == "MenuButton" {
|
||||
let b = self.state.bounds;
|
||||
match self.direction {
|
||||
0 => draw_triangle_up(target, style.text_color, b.x + b.w - 12, b.y + b.h / 2, 8),
|
||||
2 => draw_triangle_left(target, style.text_color, b.x + 12, b.y + b.h / 2, 8),
|
||||
3 => draw_triangle_right(target, style.text_color, b.x + b.w - 12, b.y + b.h / 2, 8),
|
||||
4 => {}
|
||||
_ => draw_triangle_down(target, style.text_color, b.x + b.w - 12, b.y + b.h / 2, 8),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn on_button_press(&mut self, x: i32, y: i32, button: u32) -> bool {
|
||||
button_press(&mut self.state, x, y, button)
|
||||
@@ -295,6 +321,8 @@ pub struct EntryWidget {
|
||||
pub sel_start: Option<usize>,
|
||||
pub editable: bool,
|
||||
pub password: bool,
|
||||
pub show_password: bool,
|
||||
pub cursor_visible: bool,
|
||||
pub multiline: bool,
|
||||
}
|
||||
|
||||
@@ -309,6 +337,8 @@ impl EntryWidget {
|
||||
sel_start: None,
|
||||
editable: true,
|
||||
password: false,
|
||||
show_password: false,
|
||||
cursor_visible: true,
|
||||
multiline: false,
|
||||
}
|
||||
}
|
||||
@@ -328,6 +358,18 @@ impl EntryWidget {
|
||||
pub fn set_password(&mut self, v: bool) {
|
||||
self.password = v;
|
||||
}
|
||||
pub fn get_show_password(&self) -> bool {
|
||||
self.show_password
|
||||
}
|
||||
pub fn set_show_password(&mut self, v: bool) {
|
||||
self.show_password = v;
|
||||
}
|
||||
pub fn get_cursor_visible(&self) -> bool {
|
||||
self.cursor_visible
|
||||
}
|
||||
pub fn set_cursor_visible(&mut self, v: bool) {
|
||||
self.cursor_visible = v;
|
||||
}
|
||||
pub fn get_multiline(&self) -> bool {
|
||||
self.multiline
|
||||
}
|
||||
@@ -337,6 +379,15 @@ impl EntryWidget {
|
||||
}
|
||||
|
||||
archetype_impl!(EntryWidget, Archetype::Entry,
|
||||
fn set_password(&mut self, v: bool) {
|
||||
self.password = v;
|
||||
}
|
||||
fn set_show_password(&mut self, v: bool) {
|
||||
self.show_password = v;
|
||||
}
|
||||
fn set_cursor_visible(&mut self, v: bool) {
|
||||
self.cursor_visible = v;
|
||||
}
|
||||
fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) {
|
||||
if !self.state.visible {
|
||||
return;
|
||||
@@ -344,13 +395,13 @@ archetype_impl!(EntryWidget, Archetype::Entry,
|
||||
let style = resolve_style(styles, &self.state.style_name);
|
||||
let b = self.state.bounds;
|
||||
draw_polished(target, &style, b.x, b.y, b.x + b.w, b.y + b.h);
|
||||
let shown = if self.password {
|
||||
let shown = if self.password && !self.show_password {
|
||||
"*".repeat(self.state.text.chars().count())
|
||||
} else {
|
||||
self.state.text.clone()
|
||||
};
|
||||
draw_text_left(target, &style, b.x + 4, b.y, b.y + b.h, &shown);
|
||||
if self.state.focused {
|
||||
if self.state.focused && self.cursor_visible {
|
||||
let tw = style.text_width(&shown[..self.cursor.min(shown.len())]);
|
||||
target.set_foreground(style.text_color);
|
||||
target.draw_line(b.x + 4 + tw, b.y + 3, b.x + 4 + tw, b.y + b.h - 3);
|
||||
@@ -437,6 +488,11 @@ pub struct RangeWidget {
|
||||
pub show_value: bool,
|
||||
pub digits: i32,
|
||||
pub dragging: bool,
|
||||
/// Scrollbar page size (C++ `GtkScrollbar::page_size_`, default 25).
|
||||
pub page_size: f64,
|
||||
/// Scrollbar page increment (C++ `GtkScrollbar::page_increment_`,
|
||||
/// default 10): the PgUp/PgDn step.
|
||||
pub page_increment: f64,
|
||||
}
|
||||
|
||||
impl RangeWidget {
|
||||
@@ -450,6 +506,8 @@ impl RangeWidget {
|
||||
show_value: true,
|
||||
digits: 0,
|
||||
dragging: false,
|
||||
page_size: 25.0,
|
||||
page_increment: 10.0,
|
||||
}
|
||||
}
|
||||
pub fn new_widget(name: &str, kind: &str, x: i32, y: i32, w: i32, h: i32) -> RangeWidget {
|
||||
@@ -474,6 +532,18 @@ impl RangeWidget {
|
||||
pub fn set_digits(&mut self, d: i32) {
|
||||
self.digits = d;
|
||||
}
|
||||
pub fn get_page_size(&self) -> f64 {
|
||||
self.page_size
|
||||
}
|
||||
pub fn set_page_size(&mut self, v: f64) {
|
||||
self.page_size = v;
|
||||
}
|
||||
pub fn get_page_increment(&self) -> f64 {
|
||||
self.page_increment
|
||||
}
|
||||
pub fn set_page_increment(&mut self, v: f64) {
|
||||
self.page_increment = v;
|
||||
}
|
||||
|
||||
fn frac(&self) -> f64 {
|
||||
let (mn, mx) = (self.state.min, self.state.max);
|
||||
@@ -504,6 +574,12 @@ impl RangeWidget {
|
||||
}
|
||||
|
||||
archetype_impl!(RangeWidget, Archetype::Range,
|
||||
fn set_page_size(&mut self, v: f64) {
|
||||
self.page_size = v;
|
||||
}
|
||||
fn set_page_increment(&mut self, v: f64) {
|
||||
self.page_increment = v;
|
||||
}
|
||||
fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) {
|
||||
if !self.state.visible {
|
||||
return;
|
||||
@@ -581,6 +657,14 @@ archetype_impl!(RangeWidget, Archetype::Range,
|
||||
self.state.value = self.state.max;
|
||||
true
|
||||
}
|
||||
Key::PageUp => {
|
||||
self.state.value = self.state.clamp_value(self.state.value + self.page_increment);
|
||||
true
|
||||
}
|
||||
Key::PageDown => {
|
||||
self.state.value = self.state.clamp_value(self.state.value - self.page_increment);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -651,6 +735,18 @@ impl ListSelWidget {
|
||||
}
|
||||
|
||||
archetype_impl!(ListSelWidget, Archetype::ListSel,
|
||||
fn set_items(&mut self, items: &[String]) {
|
||||
self.items = items.to_vec();
|
||||
if let Some(sel) = self.selected {
|
||||
if sel >= self.items.len() {
|
||||
self.selected = None;
|
||||
}
|
||||
}
|
||||
self.selected_set.retain(|i| *i < self.items.len());
|
||||
}
|
||||
fn set_multi_select(&mut self, v: bool) {
|
||||
self.multi = v;
|
||||
}
|
||||
fn items(&self) -> &[String] {
|
||||
&self.items
|
||||
}
|
||||
|
||||
@@ -200,6 +200,17 @@ pub trait Widget {
|
||||
}
|
||||
fn select(&mut self, _index: usize) {}
|
||||
|
||||
// -- Phase 1 generic setters (default: no-op; archetypes override
|
||||
// the ones their class supports) --
|
||||
fn set_password(&mut self, _v: bool) {}
|
||||
fn set_show_password(&mut self, _v: bool) {}
|
||||
fn set_cursor_visible(&mut self, _v: bool) {}
|
||||
fn set_direction(&mut self, _v: i32) {}
|
||||
fn set_page_increment(&mut self, _v: f64) {}
|
||||
fn set_page_size(&mut self, _v: f64) {}
|
||||
fn set_multi_select(&mut self, _v: bool) {}
|
||||
fn set_items(&mut self, _items: &[String]) {}
|
||||
|
||||
// -- containers (default: no children) --
|
||||
fn children_count(&self) -> usize {
|
||||
0
|
||||
|
||||
Reference in new issue
Block a user