.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
use std::collections::BTreeMap;
|
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::{mouse, text, Shell};
|
||||||
use iced::advanced::layout::{Limits, Node};
|
use iced::advanced::layout::{Limits, Node};
|
||||||
use iced::advanced::renderer::Style;
|
use iced::advanced::renderer::Style;
|
||||||
@@ -7,11 +7,8 @@ use iced::advanced::{Layout, Widget};
|
|||||||
use iced::advanced::widget::{tree, Tree};
|
use iced::advanced::widget::{tree, Tree};
|
||||||
use iced::mouse::{Cursor, Interaction};
|
use iced::mouse::{Cursor, Interaction};
|
||||||
use iced::widget::text_input::Catalog;
|
use iced::widget::text_input::Catalog;
|
||||||
use iced::widget::TextInput;
|
|
||||||
use tracing::info;
|
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
value: String,
|
|
||||||
control: bool,
|
control: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,21 +18,15 @@ where
|
|||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
{
|
{
|
||||||
prefixes: &'a BTreeMap<String, String>,
|
prefixes: &'a BTreeMap<String, String>,
|
||||||
placeholder: String,
|
|
||||||
iri: String,
|
|
||||||
on_control_click: Option<Message>,
|
on_control_click: Option<Message>,
|
||||||
on_input: Option<Box<dyn Fn(String) -> Message + 'a>>,
|
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> {
|
fn abbreviate(prefixes: &BTreeMap<String, String>, iri: &str) -> Option<String> {
|
||||||
for (name, base) in prefixes {
|
for (name, base) in prefixes {
|
||||||
if let Some(local_name) = iri.strip_prefix(base) {
|
if let Some(local_name) = iri.strip_prefix(base) {
|
||||||
return if local_name.is_empty() {
|
return Some(format!("{name}:{local_name}"));
|
||||||
Some(format!("{name}:"))
|
|
||||||
} else {
|
|
||||||
Some(format!("{name}:{local_name}"))
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
@@ -56,11 +47,9 @@ where
|
|||||||
{
|
{
|
||||||
pub fn new(prefixes: &'a BTreeMap<String, String>, placeholder: &str, iri: &str) -> Self {
|
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 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 {
|
Self {
|
||||||
prefixes,
|
prefixes,
|
||||||
placeholder: placeholder.to_string(),
|
|
||||||
iri: iri.to_string(),
|
|
||||||
on_control_click: None,
|
on_control_click: None,
|
||||||
on_input: None,
|
on_input: None,
|
||||||
text_input,
|
text_input,
|
||||||
@@ -72,10 +61,9 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_input(mut self, on_input: impl Fn(String) -> Message + 'a) -> Self {
|
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 wrapped = move |value: String| {
|
||||||
let expanded_value = expand(self.prefixes, &value);
|
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);
|
self.text_input = self.text_input.on_input(wrapped);
|
||||||
@@ -128,7 +116,6 @@ where
|
|||||||
|
|
||||||
fn state(&self) -> tree::State {
|
fn state(&self) -> tree::State {
|
||||||
tree::State::new(State {
|
tree::State::new(State {
|
||||||
value: String::new(),
|
|
||||||
control: false,
|
control: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -150,11 +137,6 @@ where
|
|||||||
shell.capture_event();
|
shell.capture_event();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Keyboard(keyboard::Event::KeyPressed { text, .. }) => {
|
|
||||||
if let Some(text) = text {
|
|
||||||
state.value = text.to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user