Handle some TextInput events
This commit is contained in:
parent
63cd0fd8eb
commit
fedcab6f4f
7 changed files with 93 additions and 29 deletions
|
|
@ -13,7 +13,9 @@ pub struct TextInput<'a, Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct State {}
|
pub struct State {
|
||||||
|
pub is_focused: bool,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, Message> TextInput<'a, Message> {
|
impl<'a, Message> TextInput<'a, Message> {
|
||||||
pub fn new<F>(
|
pub fn new<F>(
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
|
input::{keyboard, mouse, ButtonState},
|
||||||
Element, Event, Hasher, Layout, Length, Node, Point, Rectangle, Style,
|
Element, Event, Hasher, Layout, Length, Node, Point, Rectangle, Style,
|
||||||
Widget,
|
Widget,
|
||||||
};
|
};
|
||||||
|
|
@ -27,12 +28,47 @@ where
|
||||||
|
|
||||||
fn on_event(
|
fn on_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_event: Event,
|
event: Event,
|
||||||
_layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
_cursor_position: Point,
|
cursor_position: Point,
|
||||||
_messages: &mut Vec<Message>,
|
messages: &mut Vec<Message>,
|
||||||
_renderer: &Renderer,
|
_renderer: &Renderer,
|
||||||
) {
|
) {
|
||||||
|
match event {
|
||||||
|
Event::Mouse(mouse::Event::Input {
|
||||||
|
button: mouse::Button::Left,
|
||||||
|
state: ButtonState::Pressed,
|
||||||
|
}) => {
|
||||||
|
self.state.is_focused =
|
||||||
|
layout.bounds().contains(cursor_position);
|
||||||
|
}
|
||||||
|
Event::Keyboard(keyboard::Event::CharacterReceived(c))
|
||||||
|
if self.state.is_focused && !c.is_control() =>
|
||||||
|
{
|
||||||
|
self.value.push(c);
|
||||||
|
|
||||||
|
let message = (self.on_change)(self.value.clone());
|
||||||
|
messages.push(message);
|
||||||
|
}
|
||||||
|
Event::Keyboard(keyboard::Event::Input {
|
||||||
|
key_code: keyboard::KeyCode::Backspace,
|
||||||
|
state: ButtonState::Pressed,
|
||||||
|
}) => {
|
||||||
|
let _ = self.value.pop();
|
||||||
|
|
||||||
|
let message = (self.on_change)(self.value.clone());
|
||||||
|
messages.push(message);
|
||||||
|
}
|
||||||
|
Event::Keyboard(keyboard::Event::Input {
|
||||||
|
key_code: keyboard::KeyCode::Enter,
|
||||||
|
state: ButtonState::Pressed,
|
||||||
|
}) => {
|
||||||
|
if let Some(on_submit) = self.on_submit.clone() {
|
||||||
|
messages.push(on_submit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(
|
fn draw(
|
||||||
|
|
@ -78,7 +114,9 @@ where
|
||||||
Renderer: 'static + self::Renderer,
|
Renderer: 'static + self::Renderer,
|
||||||
Message: 'static + Clone + std::fmt::Debug,
|
Message: 'static + Clone + std::fmt::Debug,
|
||||||
{
|
{
|
||||||
fn from(button: TextInput<'a, Message>) -> Element<'a, Message, Renderer> {
|
fn from(
|
||||||
Element::new(button)
|
text_input: TextInput<'a, Message>,
|
||||||
|
) -> Element<'a, Message, Renderer> {
|
||||||
|
Element::new(text_input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ const SIZE: f32 = 28.0;
|
||||||
impl checkbox::Renderer for Renderer {
|
impl checkbox::Renderer for Renderer {
|
||||||
fn node<Message>(&self, checkbox: &Checkbox<Message>) -> Node {
|
fn node<Message>(&self, checkbox: &Checkbox<Message>) -> Node {
|
||||||
Row::<(), Self>::new()
|
Row::<(), Self>::new()
|
||||||
|
.width(Length::Fill)
|
||||||
.spacing(15)
|
.spacing(15)
|
||||||
.align_items(Align::Center)
|
.align_items(Align::Center)
|
||||||
.push(
|
.push(
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use iced_native::{
|
||||||
};
|
};
|
||||||
|
|
||||||
const SCROLLBAR_WIDTH: u16 = 10;
|
const SCROLLBAR_WIDTH: u16 = 10;
|
||||||
const SCROLLBAR_MARGIN: u16 = 10;
|
const SCROLLBAR_MARGIN: u16 = 5;
|
||||||
|
|
||||||
fn scrollbar_bounds(bounds: Rectangle) -> Rectangle {
|
fn scrollbar_bounds(bounds: Rectangle) -> Rectangle {
|
||||||
Rectangle {
|
Rectangle {
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ impl text::Renderer for Renderer {
|
||||||
let (width, height) = if let Some(bounds) =
|
let (width, height) = if let Some(bounds) =
|
||||||
glyph_brush.borrow_mut().glyph_bounds(&text)
|
glyph_brush.borrow_mut().glyph_bounds(&text)
|
||||||
{
|
{
|
||||||
(bounds.width(), bounds.height())
|
(bounds.width().round(), bounds.height().round())
|
||||||
} else {
|
} else {
|
||||||
(0.0, 0.0)
|
(0.0, 0.0)
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -23,21 +23,23 @@ impl text_input::Renderer for Renderer {
|
||||||
|
|
||||||
let border = Primitive::Quad {
|
let border = Primitive::Quad {
|
||||||
bounds,
|
bounds,
|
||||||
background: Background::Color(if is_mouse_over {
|
background: Background::Color(
|
||||||
Color {
|
if is_mouse_over || text_input.state.is_focused {
|
||||||
r: 0.5,
|
Color {
|
||||||
g: 0.5,
|
r: 0.5,
|
||||||
b: 0.5,
|
g: 0.5,
|
||||||
a: 1.0,
|
b: 0.5,
|
||||||
}
|
a: 1.0,
|
||||||
} else {
|
}
|
||||||
Color {
|
} else {
|
||||||
r: 0.7,
|
Color {
|
||||||
g: 0.7,
|
r: 0.7,
|
||||||
b: 0.7,
|
g: 0.7,
|
||||||
a: 1.0,
|
b: 0.7,
|
||||||
}
|
a: 1.0,
|
||||||
}),
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
border_radius: 5,
|
border_radius: 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -70,9 +72,9 @@ impl text_input::Renderer for Renderer {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Color {
|
Color {
|
||||||
r: 0.9,
|
r: 0.3,
|
||||||
g: 0.9,
|
g: 0.3,
|
||||||
b: 0.9,
|
b: 0.3,
|
||||||
a: 1.0,
|
a: 1.0,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
column, conversion, input::mouse, renderer::Windowed, Cache, Column,
|
column, conversion,
|
||||||
Element, Event, Length, MouseCursor, UserInterface,
|
input::{keyboard, mouse},
|
||||||
|
renderer::Windowed,
|
||||||
|
Cache, Column, Element, Event, Length, MouseCursor, UserInterface,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Application {
|
pub trait Application {
|
||||||
|
|
@ -167,6 +169,25 @@ pub trait Application {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
WindowEvent::ReceivedCharacter(c) => {
|
||||||
|
events.push(Event::Keyboard(
|
||||||
|
keyboard::Event::CharacterReceived(c),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
winit::event::KeyboardInput {
|
||||||
|
virtual_keycode: Some(virtual_keycode),
|
||||||
|
state,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
events.push(Event::Keyboard(keyboard::Event::Input {
|
||||||
|
key_code: conversion::key_code(virtual_keycode),
|
||||||
|
state: conversion::button_state(state),
|
||||||
|
}));
|
||||||
|
}
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue