better global state structure?
This commit is contained in:
@@ -4,12 +4,15 @@ use winit::dpi::{LogicalPosition, LogicalSize};
|
||||
|
||||
pub struct Selector;
|
||||
|
||||
impl<State: HasUiState + HasEvents, W: Widget + 'static> WidgetAttr<State, W> for Selector {
|
||||
impl<State: HasEvents, W: Widget + 'static> WidgetAttr<State, W> for Selector
|
||||
where
|
||||
State::State: HasDefaultUiState,
|
||||
{
|
||||
type Input = WidgetRef<TextEdit>;
|
||||
|
||||
fn run(state: &mut State, container: WidgetRef<W>, id: Self::Input) {
|
||||
state.register_event(container, CursorSense::click_or_drag(), move |ctx| {
|
||||
let region = ctx.state.ui().window_region(&id).unwrap();
|
||||
let region = ctx.ui().window_region(&id).unwrap();
|
||||
let id_pos = region.top_left;
|
||||
let container_pos = ctx.state.ui().window_region(&container).unwrap().top_left;
|
||||
let pos = ctx.data.pos + container_pos - id_pos;
|
||||
@@ -21,7 +24,10 @@ impl<State: HasUiState + HasEvents, W: Widget + 'static> WidgetAttr<State, W> fo
|
||||
|
||||
pub struct Selectable;
|
||||
|
||||
impl<State: HasUiState + HasEvents> WidgetAttr<State, TextEdit> for Selectable {
|
||||
impl<State: HasEvents> WidgetAttr<State, TextEdit> for Selectable
|
||||
where
|
||||
State::State: HasDefaultUiState,
|
||||
{
|
||||
type Input = ();
|
||||
|
||||
fn run(state: &mut State, id: WidgetRef<TextEdit>, _: Self::Input) {
|
||||
@@ -38,22 +44,23 @@ impl<State: HasUiState + HasEvents> WidgetAttr<State, TextEdit> for Selectable {
|
||||
}
|
||||
|
||||
fn select(
|
||||
state: &mut impl HasUiState,
|
||||
state: &mut impl HasDefaultUiState,
|
||||
id: WidgetRef<TextEdit>,
|
||||
pos: Vec2,
|
||||
size: Vec2,
|
||||
dragging: bool,
|
||||
) {
|
||||
let (ui, state) = HasDefaultUiState::ui_with_state(state);
|
||||
let now = Instant::now();
|
||||
let recent = (now - state.ui_state().last_click) < Duration::from_millis(300);
|
||||
state.ui_state().last_click = now;
|
||||
id.edit(state.ui()).select(pos, size, dragging, recent);
|
||||
if let Some(region) = state.ui().window_region(&id) {
|
||||
state.ui_state().window.set_ime_allowed(true);
|
||||
state.ui_state().window.set_ime_cursor_area(
|
||||
let recent = (now - state.last_click) < Duration::from_millis(300);
|
||||
state.last_click = now;
|
||||
id.edit(ui).select(pos, size, dragging, recent);
|
||||
if let Some(region) = ui.window_region(&id) {
|
||||
state.window.set_ime_allowed(true);
|
||||
state.window.set_ime_cursor_area(
|
||||
LogicalPosition::<f32>::from(region.top_left.tuple()),
|
||||
LogicalSize::<f32>::from(region.size().tuple()),
|
||||
);
|
||||
}
|
||||
state.ui_state().focus = Some(id);
|
||||
state.focus = Some(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user