This commit is contained in:
Alex Wied
2026-06-29 12:51:38 -04:00
parent 2ef8a74b5f
commit ee5deb87c1
+5 -23
View File
@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use iced::{keyboard, Element, Event, Length, Rectangle, Size};
use iced::{keyboard, widget, Element, Event, Length, Rectangle, Size};
use iced::advanced::{mouse, text, Shell};
use iced::advanced::layout::{Limits, Node};
use iced::advanced::renderer::Style;
@@ -7,11 +7,8 @@ use iced::advanced::{Layout, Widget};
use iced::advanced::widget::{tree, Tree};
use iced::mouse::{Cursor, Interaction};
use iced::widget::text_input::Catalog;
use iced::widget::TextInput;
use tracing::info;
pub struct State {
value: String,
control: bool,
}
@@ -21,21 +18,15 @@ where
Renderer: text::Renderer,
{
prefixes: &'a BTreeMap<String, String>,
placeholder: String,
iri: String,
on_control_click: Option<Message>,
on_input: Option<Box<dyn Fn(String) -> Message + 'a>>,
text_input: TextInput<'a, Message, Theme, Renderer>,
text_input: widget::TextInput<'a, Message, Theme, Renderer>,
}
fn abbreviate(prefixes: &BTreeMap<String, String>, iri: &str) -> Option<String> {
for (name, base) in prefixes {
if let Some(local_name) = iri.strip_prefix(base) {
return if local_name.is_empty() {
Some(format!("{name}:"))
} else {
Some(format!("{name}:{local_name}"))
};
return Some(format!("{name}:{local_name}"));
}
}
None
@@ -56,11 +47,9 @@ where
{
pub fn new(prefixes: &'a BTreeMap<String, String>, placeholder: &str, iri: &str) -> Self {
let display_value = abbreviate(prefixes, iri).unwrap_or_else(|| iri.to_string());
let text_input = TextInput::new(placeholder, &display_value);
let text_input = widget::TextInput::new(placeholder, &display_value);
Self {
prefixes,
placeholder: placeholder.to_string(),
iri: iri.to_string(),
on_control_click: None,
on_input: None,
text_input,
@@ -72,10 +61,9 @@ where
}
pub fn on_input(mut self, on_input: impl Fn(String) -> Message + 'a) -> Self {
let iri = self.iri.clone();
let wrapped = move |value: String| {
let expanded_value = expand(self.prefixes, &value);
on_input(expanded_value.unwrap_or_else(|| iri.clone()))
on_input(expanded_value.unwrap_or_else(|| value))
};
self.text_input = self.text_input.on_input(wrapped);
@@ -128,7 +116,6 @@ where
fn state(&self) -> tree::State {
tree::State::new(State {
value: String::new(),
control: false,
})
}
@@ -150,11 +137,6 @@ where
shell.capture_event();
}
}
Event::Keyboard(keyboard::Event::KeyPressed { text, .. }) => {
if let Some(text) = text {
state.value = text.to_string();
}
}
_ => {}
}