add offset / scrolling + clipboard support

This commit is contained in:
iris committed 2025-09-27 21:13:00 -04:00
1 parent 95f049acb4
commit 5445008528
16 files changed
+386 -99

No files matched your search

+9 -2
View File
@@ -3,7 +3,7 @@ use ui::{
layout::Vec2,
};
use winit::{
event::{MouseButton, WindowEvent},
event::{MouseButton, MouseScrollDelta, WindowEvent},
keyboard::{Key, NamedKey},
};
@@ -32,6 +32,13 @@ impl Input {
_ => (),
}
}
WindowEvent::MouseWheel { delta, .. } => {
let delta = match *delta {
MouseScrollDelta::LineDelta(x, y) => Vec2::new(x, y),
MouseScrollDelta::PixelDelta(pos) => Vec2::new(pos.x as f32, pos.y as f32),
};
self.cursor.scroll_delta = delta;
}
WindowEvent::CursorLeft { .. } => {
self.cursor.exists = false;
self.modifiers.clear();
@@ -56,7 +63,7 @@ impl Input {
}
pub fn end_frame(&mut self) {
self.cursor.buttons.end_frame();
self.cursor.end_frame();
}
}
+21 -3
View File
@@ -1,6 +1,7 @@
use std::sync::Arc;
use app::App;
use arboard::Clipboard;
use cosmic_text::Family;
use render::Renderer;
use ui::prelude::*;
@@ -22,6 +23,7 @@ pub struct Client {
ui: Ui,
info: WidgetId<Text>,
focus: Option<WidgetId<TextEdit>>,
clipboard: Clipboard,
}
#[derive(Eq, PartialEq, Hash, Clone)]
@@ -127,6 +129,15 @@ impl Client {
.add_static(&mut ui);
let texts = Span::empty(Dir::DOWN).add_static(&mut ui);
let msg_area = (
Rect::new(Color::SKY),
texts
.offset(UiVec2::ZERO)
.edit_on(Sense::Scroll, |w, data| {
w.amt += UiVec2::abs(data.scroll_delta * 50.0);
}),
)
.stack();
let add_text = text_edit("add")
.text_align(Align::Left)
.size(30)
@@ -151,7 +162,7 @@ impl Client {
})
.add(&mut ui);
let text_edit_scroll = (
(Rect::new(Color::SKY), texts).stack(),
msg_area,
(
Rect::new(Color::WHITE.darker(0.9)),
(
@@ -217,6 +228,7 @@ impl Client {
ui,
info,
focus: None,
clipboard: Clipboard::new().unwrap(),
}
}
@@ -249,14 +261,20 @@ impl Client {
if let Some(sel) = &self.focus
&& event.state.is_pressed()
{
match self.ui.text(sel).apply_event(&event, &self.input.modifiers) {
let mut text = self.ui.text(sel);
match text.apply_event(&event, &self.input.modifiers) {
TextInputResult::Unfocus => {
self.focus = None;
}
TextInputResult::Submit => {
self.run_event(&sel.clone(), Submit, ());
}
_ => (),
TextInputResult::Paste => {
if let Ok(t) = self.clipboard.get_text() {
text.insert(&t);
}
}
TextInputResult::Unused | TextInputResult::Used => (),
}
}
}