Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions src/bin/edit/draw_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppress whitespace changes: https://github.com/microsoft/edit/pull/267/files?w=1

This change in particular ensures that if we ever remove documents programmatically, the Go to Line/Column dialog isn't just closed but its state also fully cleaned up.

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<Point, ParseIntError> {
let parts = line.split_once(':').unwrap_or((line, "0"));
let line = parts.0.parse::<i32>()?;
let column = parts.1.parse::<i32>()?;
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::<CoordType>()?.saturating_sub(1);
}
Ok(Point { x: coords[0], y: coords[1] })
}
18 changes: 6 additions & 12 deletions src/bin/edit/draw_menubar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added fix: Making the document-picker button a button.

loc(LocId::ViewDocumentPicker),
'P',
kbmod::CTRL | vk::P,
state.wants_document_picker,
) {
state.wants_document_picker = !state.wants_document_picker;
};
}

ctx.menubar_menu_end();
Expand Down