diff --git a/src/bin/edit/draw_editor.rs b/src/bin/edit/draw_editor.rs index a0047bf15ea9..ef7194da1465 100644 --- a/src/bin/edit/draw_editor.rs +++ b/src/bin/edit/draw_editor.rs @@ -275,49 +275,55 @@ pub fn draw_handle_wants_close(ctx: &mut Context, state: &mut State) { } pub fn draw_goto_menu(ctx: &mut Context, state: &mut State) { - let Some(doc) = state.documents.active_mut() else { - // Goto does not make sense if there is no active document - state.wants_goto = false; - return; - }; let mut done = false; - ctx.modal_begin("goto", loc(LocId::FileGoto)); - { - if ctx.editline("goto-line", &mut state.goto_target) { - state.goto_invalid = false; - } - if state.goto_invalid { - ctx.attr_background_rgba(ctx.indexed(IndexedColor::Red)); - ctx.attr_foreground_rgba(ctx.indexed(IndexedColor::BrightWhite)); - } + if let Some(doc) = state.documents.active_mut() { + ctx.modal_begin("goto", loc(LocId::FileGoto)); + { + if ctx.editline("goto-line", &mut state.goto_target) { + state.goto_invalid = false; + } + if state.goto_invalid { + ctx.attr_background_rgba(ctx.indexed(IndexedColor::Red)); + ctx.attr_foreground_rgba(ctx.indexed(IndexedColor::BrightWhite)); + } - ctx.attr_intrinsic_size(Size { width: 24, height: 1 }); - ctx.steal_focus(); + ctx.attr_intrinsic_size(Size { width: 24, height: 1 }); + ctx.steal_focus(); - if ctx.consume_shortcut(vk::RETURN) { - let mut buf = doc.buffer.borrow_mut(); - match validate_goto_point(&state.goto_target) { - Ok(point) => { - buf.cursor_move_to_logical(point); - buf.make_cursor_visible(); - done = true; + if ctx.consume_shortcut(vk::RETURN) { + match validate_goto_point(&state.goto_target) { + Ok(point) => { + let mut buf = doc.buffer.borrow_mut(); + buf.cursor_move_to_logical(point); + buf.make_cursor_visible(); + done = true; + } + Err(_) => state.goto_invalid = true, } - Err(_) => state.goto_invalid = true, + ctx.needs_rerender(); } - ctx.needs_rerender(); } + done |= ctx.modal_end(); + } else { + done = true; } - if ctx.modal_end() || done { + + if done { state.wants_goto = false; state.goto_target.clear(); state.goto_invalid = false; + ctx.needs_rerender(); } } fn validate_goto_point(line: &str) -> Result { - let parts = line.split_once(':').unwrap_or((line, "0")); - let line = parts.0.parse::()?; - let column = parts.1.parse::()?; - Ok(Point { y: line.saturating_sub(1), x: column.saturating_sub(1) }) + let mut coords = [0; 2]; + let (y, x) = line.split_once(':').unwrap_or((line, "0")); + // Using a loop here avoids 2 copies of the str->int code. + // This makes the binary more compact. + for (i, s) in [x, y].iter().enumerate() { + coords[i] = s.parse::()?.saturating_sub(1); + } + Ok(Point { x: coords[0], y: coords[1] }) } diff --git a/src/bin/edit/draw_menubar.rs b/src/bin/edit/draw_menubar.rs index 9fa761b5bed3..e6d1e2fd2ac1 100644 --- a/src/bin/edit/draw_menubar.rs +++ b/src/bin/edit/draw_menubar.rs @@ -56,9 +56,6 @@ fn draw_menu_file(ctx: &mut Context, state: &mut State) { if ctx.menubar_menu_button(loc(LocId::FileExit), 'X', kbmod::CTRL | vk::Q) { state.wants_exit = true; } - if ctx.menubar_menu_button(loc(LocId::FileGoto), 'G', kbmod::CTRL | vk::G) { - state.wants_goto = true; - } ctx.menubar_menu_end(); } @@ -106,19 +103,16 @@ fn draw_menu_view(ctx: &mut Context, state: &mut State) { let mut tb = doc.buffer.borrow_mut(); let word_wrap = tb.is_word_wrap_enabled(); + if ctx.menubar_menu_button(loc(LocId::ViewDocumentPicker), 'P', kbmod::CTRL | vk::P) { + state.wants_document_picker = true; + } + if ctx.menubar_menu_button(loc(LocId::FileGoto), 'G', kbmod::CTRL | vk::G) { + state.wants_goto = true; + } if ctx.menubar_menu_checkbox(loc(LocId::ViewWordWrap), 'W', kbmod::ALT | vk::Z, word_wrap) { tb.set_word_wrap(!word_wrap); ctx.needs_rerender(); } - - if ctx.menubar_menu_checkbox( - loc(LocId::ViewDocumentPicker), - 'P', - kbmod::CTRL | vk::P, - state.wants_document_picker, - ) { - state.wants_document_picker = !state.wants_document_picker; - }; } ctx.menubar_menu_end();