strong & weak widgets

This commit is contained in:
2025-12-11 07:16:06 -05:00
parent a85e129026
commit 36668c82f4
19 changed files with 293 additions and 294 deletions

View File

@@ -5,23 +5,18 @@ use winit::dpi::{LogicalPosition, LogicalSize};
pub struct Selector;
impl<W: 'static + Widget> WidgetAttr<W> for Selector {
type Input = WidgetHandle<TextEdit>;
type Input = WidgetView<TextEdit>;
fn run(ui: &mut Ui, container: &WidgetHandle<W>, id: Self::Input) {
let container = container.clone();
ui.register_event(
&container.clone(),
CursorSense::click_or_drag(),
move |mut ctx| {
let ui = ctx.ui;
let region = ui.window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ui.window_region(&container).unwrap().top_left;
ctx.data.cursor += container_pos - id_pos;
ctx.data.size = region.size();
select(ui, id.clone(), ctx.state, ctx.data);
},
);
fn run(ui: &mut Ui, container: WidgetView<W>, id: Self::Input) {
ui.register_event(&container, CursorSense::click_or_drag(), move |mut ctx| {
let ui = ctx.ui;
let region = ui.window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ui.window_region(&container).unwrap().top_left;
ctx.data.cursor += container_pos - id_pos;
ctx.data.size = region.size();
select(ui, id, ctx.state, ctx.data);
});
}
}
@@ -30,15 +25,14 @@ pub struct Selectable;
impl WidgetAttr<TextEdit> for Selectable {
type Input = ();
fn run(ui: &mut Ui, id: &WidgetHandle<TextEdit>, _: Self::Input) {
let id = id.clone();
ui.register_event(&id.clone(), CursorSense::click_or_drag(), move |ctx| {
select(ctx.ui, id.clone(), ctx.state, ctx.data);
fn run(ui: &mut Ui, id: WidgetView<TextEdit>, _: Self::Input) {
ui.register_event(&id, CursorSense::click_or_drag(), move |ctx| {
select(ctx.ui, id, ctx.state, ctx.data);
});
}
}
fn select(ui: &mut Ui, id: WidgetHandle<TextEdit>, state: &mut UiState, data: CursorData) {
fn select(ui: &mut Ui, id: WidgetView<TextEdit>, state: &mut UiState, data: CursorData) {
let now = Instant::now();
let recent = (now - state.last_click) < Duration::from_millis(300);
state.last_click = now;

View File

@@ -30,7 +30,7 @@ pub struct DefaultState<AppState> {
pub struct UiState {
pub renderer: UiRenderer,
pub input: Input,
pub focus: Option<WidgetHandle<TextEdit>>,
pub focus: Option<WidgetView<TextEdit>>,
pub clipboard: Clipboard,
pub window: Arc<Window>,
pub ime: usize,
@@ -91,7 +91,7 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
let input_changed = ui_state.input.event(&event);
let cursor_state = ui_state.cursor_state().clone();
let old = ui_state.focus.clone();
let old = ui_state.focus;
if cursor_state.buttons.left.is_start() {
ui_state.focus = None;
}
@@ -121,10 +121,9 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
ui_state.renderer.resize(size)
}
WindowEvent::KeyboardInput { event, .. } => {
if let Some(sel) = &ui_state.focus
if let Some(sel) = ui_state.focus
&& event.state.is_pressed()
{
let sel = &sel.clone();
let mut text = sel.edit(ui);
match text.apply_event(event, &ui_state.input.modifiers) {
TextInputResult::Unfocus => {