fyrox_ui/path.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
//! Path editor is a simple widget that has a text box, that shows the current path and a "..." button, that opens a file
//! selector. See [`PathEditor`] docs for more info and usage examples.
#![warn(missing_docs)]
use crate::{
button::{ButtonBuilder, ButtonMessage},
core::{pool::Handle, reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*},
define_constructor,
file_browser::{FileSelectorBuilder, FileSelectorMessage},
grid::{Column, GridBuilder, Row},
message::{MessageDirection, UiMessage},
text::TextMessage,
text_box::TextBoxBuilder,
widget::{Widget, WidgetBuilder, WidgetMessage},
window::{WindowBuilder, WindowMessage, WindowTitle},
BuildContext, Control, Thickness, UiNode, UserInterface,
};
use fyrox_core::uuid_provider;
use fyrox_core::variable::InheritableVariable;
use std::{
ops::{Deref, DerefMut},
path::Path,
path::PathBuf,
};
/// A set of messages for the [`PathEditor`] widget.
#[derive(Debug, Clone, PartialEq)]
pub enum PathEditorMessage {
/// A message, that is used to set new value of the editor or to receive changes from the editor.
Path(PathBuf),
}
impl PathEditorMessage {
define_constructor!(
/// Creates [`PathEditorMessage::Path`] message.
PathEditorMessage:Path => fn path(PathBuf), layout: false
);
}
/// Path editor is a simple widget that has a text box, that shows the current path and a "..." button, that opens a file
/// selector.
///
/// ## Examples
///
/// An instance of the editor could be created like so:
///
/// ```rust
/// # use fyrox_ui::{
/// # core::pool::Handle, path::PathEditorBuilder, widget::WidgetBuilder, BuildContext, UiNode,
/// # };
/// # use std::path::PathBuf;
/// #
/// fn create_path_editor(path: PathBuf, ctx: &mut BuildContext) -> Handle<UiNode> {
/// PathEditorBuilder::new(WidgetBuilder::new())
/// .with_path(path)
/// .build(ctx)
/// }
/// ```
///
/// To receive the changes, listen to [`PathEditorMessage::Path`] and check for its direction, it should be [`MessageDirection::FromWidget`].
/// To set a new path value, send [`PathEditorMessage::Path`] message, but with [`MessageDirection::ToWidget`].
#[derive(Default, Clone, Visit, Reflect, Debug, ComponentProvider)]
pub struct PathEditor {
/// Base widget of the editor.
pub widget: Widget,
/// A handle of the text field, that is used to show current path.
pub text_field: InheritableVariable<Handle<UiNode>>,
/// A button, that opens a file selection.
pub select: InheritableVariable<Handle<UiNode>>,
/// Current file selector instance, could be [`Handle::NONE`] if the selector is closed.
pub selector: InheritableVariable<Handle<UiNode>>,
/// Current path.
pub path: InheritableVariable<PathBuf>,
}
crate::define_widget_deref!(PathEditor);
uuid_provider!(PathEditor = "51cfe7ec-ec31-4354-9578-047004b213a1");
impl Control for PathEditor {
fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
self.widget.handle_routed_message(ui, message);
if let Some(ButtonMessage::Click) = message.data() {
if message.destination() == *self.select {
self.selector.set_value_and_mark_modified(
FileSelectorBuilder::new(
WindowBuilder::new(
WidgetBuilder::new().with_width(300.0).with_height(450.0),
)
.open(false)
.with_title(WindowTitle::text("Select a Path")),
)
.build(&mut ui.build_ctx()),
);
ui.send_message(FileSelectorMessage::path(
*self.selector,
MessageDirection::ToWidget,
(*self.path).clone(),
));
ui.send_message(WindowMessage::open_modal(
*self.selector,
MessageDirection::ToWidget,
true,
true,
));
}
} else if let Some(PathEditorMessage::Path(path)) = message.data() {
if message.destination() == self.handle
&& message.direction() == MessageDirection::ToWidget
&& &*self.path != path
{
self.path.set_value_and_mark_modified(path.clone());
ui.send_message(TextMessage::text(
*self.text_field,
MessageDirection::ToWidget,
path.to_string_lossy().to_string(),
));
ui.send_message(message.reverse());
}
}
}
fn preview_message(&self, ui: &UserInterface, message: &mut UiMessage) {
if let Some(FileSelectorMessage::Commit(path)) = message.data() {
if message.destination() == *self.selector && &*self.path != path {
ui.send_message(WidgetMessage::remove(
*self.selector,
MessageDirection::ToWidget,
));
ui.send_message(PathEditorMessage::path(
self.handle,
MessageDirection::ToWidget,
path.clone(),
));
}
}
}
}
/// Path editor builder creates [`PathEditor`] instances and adds them to the user interface.
pub struct PathEditorBuilder {
widget_builder: WidgetBuilder,
path: PathBuf,
}
impl PathEditorBuilder {
/// Creates new builder instance.
pub fn new(widget_builder: WidgetBuilder) -> Self {
Self {
widget_builder,
path: Default::default(),
}
}
/// Sets the desired path.
pub fn with_path<P: AsRef<Path>>(mut self, path: P) -> Self {
path.as_ref().clone_into(&mut self.path);
self
}
/// Finishes widget building and adds it to the user interface returning a handle to the instance.
pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
let text_field;
let select;
let grid = GridBuilder::new(
WidgetBuilder::new()
.with_child({
text_field = TextBoxBuilder::new(
WidgetBuilder::new()
.on_column(0)
.with_margin(Thickness::uniform(1.0)),
)
.with_text(self.path.to_string_lossy())
.with_editable(false)
.build(ctx);
text_field
})
.with_child({
select = ButtonBuilder::new(
WidgetBuilder::new()
.on_column(1)
.with_width(30.0)
.with_margin(Thickness::uniform(1.0)),
)
.with_text("...")
.build(ctx);
select
}),
)
.add_row(Row::stretch())
.add_column(Column::stretch())
.add_column(Column::auto())
.build(ctx);
let canvas = PathEditor {
widget: self
.widget_builder
.with_child(grid)
.with_preview_messages(true)
.build(),
text_field: text_field.into(),
select: select.into(),
selector: Default::default(),
path: self.path.into(),
};
ctx.add_node(UiNode::new(canvas))
}
}