Skip to content

Commit

Permalink
Leave text field in edit mode when name is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Procrat committed Apr 24, 2023
1 parent bb04379 commit b5c1432
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/gui/view/graph-editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ indexmap = "1.9.2"
js-sys = { workspace = true }
nalgebra = { workspace = true }
ordered-float = { workspace = true }
parser = { path = "../../language/parser" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = { workspace = true }
serde_json = { workspace = true }
Expand Down
5 changes: 5 additions & 0 deletions app/gui/view/graph-editor/src/component/breadcrumbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ ensogl::define_endpoints! {
project_name_hovered (bool),
/// Indicates whether the project name was clicked.
project_mouse_down (),
/// Signalizes an error if the user tried to rename the project to an invalid name.
project_name_error (String),
}
}

Expand Down Expand Up @@ -476,6 +478,9 @@ impl Breadcrumbs {

eval frp.input.set_project_changed((v) model.project_name.set_project_changed(v));

frp.source.project_name_error <+ model.project_name.error;


// === User Interaction ===

eval_ model.project_name.frp.output.mouse_down(frp.select_breadcrumb.emit(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ensogl::DEPRECATED_Animation;
use ensogl_component::text;
use ensogl_component::text::formatting::Size as TextSize;
use ensogl_hardcoded_theme::graph_editor::breadcrumbs as breadcrumbs_theme;
use parser::Parser;



Expand Down Expand Up @@ -66,8 +67,8 @@ ensogl::define_endpoints_2! {
cancel_editing (),
/// Enable editing the project name field and add a cursor at the mouse position.
start_editing (),
/// Commit current project name.
commit (),
/// Try committing current project name.
try_commit (),
outside_press (),
/// Indicates that this is the currently active breadcrumb.
select (),
Expand All @@ -88,6 +89,7 @@ ensogl::define_endpoints_2! {
edit_mode (bool),
selected (bool),
is_hovered (bool),
error (String),
}
}

Expand Down Expand Up @@ -214,6 +216,24 @@ impl ProjectNameModel {
self.commit(name);
}

/// Confirm the given name as the current project name if it's valid.
fn try_commit(&self, name: impl Str) -> Result<(), String> {
let name = name.into();
Self::validate(&name)
.map_err(|error| format!("The project couldn't be renamed. {error}"))?;
self.commit(name);
Ok(())
}

/// Check whether the given name is a valid project name.
fn validate(name: impl Str) -> Result<(), String> {
let parser = Parser::new();
match parser.parse_line_ast(name).map(|ast| ast.shape().clone()) {
Ok(ast::Shape::Cons(_)) => Ok(()),
_ => Err("The project name should be in Upper_Snake_Case.".to_owned()),
}
}

/// Confirm the given name as the current project name.
fn commit<T: Into<String>>(&self, name: T) {
let name = name.into();
Expand Down Expand Up @@ -316,11 +336,15 @@ impl ProjectName {

// === Commit ===

do_commit <- any(&frp.commit,&frp.outside_press).gate(&frp.output.edit_mode);
commit_text <- text_content.sample(&do_commit);
output.name <+ commit_text;
eval commit_text((text) model.commit(text));
on_commit <- commit_text.constant(());
try_commit <- any(&frp.try_commit, &frp.outside_press).gate(&frp.output.edit_mode);
commit_result <- try_commit.map2(&text_content, f!([model] (_, text) {
let result = model.try_commit(text);
(result.as_ref().ok().copied(), result.err())
}));
commit_success <- commit_result.filter_map(|(ok, _)| *ok);
commit_failure <- commit_result.filter_map(|(_, error)| error.clone());
output.name <+ text_content.sample(&commit_success);
output.error <+ commit_failure;

not_selected <- frp.output.selected.map(|selected| !selected);
on_deselect <- not_selected.gate(&not_selected).constant(());
Expand All @@ -330,7 +354,7 @@ impl ProjectName {
// === Selection ===

output.selected <+ frp.select.to_true();
set_inactive <- any(&frp.deselect,&on_commit);
set_inactive <- any(&frp.deselect, &commit_success);
eval_ set_inactive ([text,model] {
text.deprecated_set_focus(false);
text.remove_all_cursors();
Expand Down Expand Up @@ -402,7 +426,7 @@ impl View for ProjectName {
fn default_shortcuts() -> Vec<shortcut::Shortcut> {
use shortcut::ActionType::*;
[
(Press, "", "enter", "commit"),
(Press, "", "enter", "try_commit"),
(Release, "", "escape", "cancel_editing"),
(DoublePress, "is_hovered", "left-mouse-button", "start_editing"),
]
Expand Down
4 changes: 4 additions & 0 deletions app/gui/view/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ impl View {

model.debug_mode_popup.enabled <+ frp.enable_debug_mode;
model.debug_mode_popup.disabled <+ frp.disable_debug_mode;

// === Error Pop-up ===

model.popup.set_label <+ model.graph_editor.model.breadcrumbs.project_name_error;
}

init.emit(());
Expand Down

0 comments on commit b5c1432

Please sign in to comment.