Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make menus process events before updating working details #799

Merged
merged 3 commits into from
Jul 6, 2024
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
72 changes: 36 additions & 36 deletions src/menu/columnar_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,42 @@ impl Menu for ColumnarMenu {
painter: &Painter,
) {
if let Some(event) = self.event.take() {
// The working value for the menu are updated first before executing any of the
// menu events
match event {
MenuEvent::Activate(updated) => {
self.active = true;
self.reset_position();

self.input = if self.settings.only_buffer_difference {
Some(editor.get_buffer().to_string())
} else {
None
};

if !updated {
self.update_values(editor, completer);
}
}
MenuEvent::Deactivate => self.active = false,
MenuEvent::Edit(updated) => {
self.reset_position();

if !updated {
self.update_values(editor, completer);
}
}
MenuEvent::NextElement => self.move_next(),
MenuEvent::PreviousElement => self.move_previous(),
MenuEvent::MoveUp => self.move_up(),
MenuEvent::MoveDown => self.move_down(),
MenuEvent::MoveLeft => self.move_left(),
MenuEvent::MoveRight => self.move_right(),
MenuEvent::PreviousPage | MenuEvent::NextPage => {
// The columnar menu doest have the concept of pages, yet
}
}

// The working value for the menu are updated only after executing the menu events,
// so they have the latest suggestions
//
// If there is at least one suggestion that contains a description, then the layout
// is changed to one column to fit the description
Expand Down Expand Up @@ -572,40 +606,6 @@ impl Menu for ColumnarMenu {
self.working_details.columns = possible_cols;
}
}

match event {
MenuEvent::Activate(updated) => {
self.active = true;
self.reset_position();

self.input = if self.settings.only_buffer_difference {
Some(editor.get_buffer().to_string())
} else {
None
};

if !updated {
self.update_values(editor, completer);
}
}
MenuEvent::Deactivate => self.active = false,
MenuEvent::Edit(updated) => {
self.reset_position();

if !updated {
self.update_values(editor, completer);
}
}
MenuEvent::NextElement => self.move_next(),
MenuEvent::PreviousElement => self.move_previous(),
MenuEvent::MoveUp => self.move_up(),
MenuEvent::MoveDown => self.move_down(),
MenuEvent::MoveLeft => self.move_left(),
MenuEvent::MoveRight => self.move_right(),
MenuEvent::PreviousPage | MenuEvent::NextPage => {
// The columnar menu doest have the concept of pages, yet
}
}
}
}

Expand Down
98 changes: 48 additions & 50 deletions src/menu/description_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,56 +462,6 @@ impl Menu for DescriptionMenu {
painter: &Painter,
) {
if let Some(event) = self.event.take() {
// Updating all working parameters from the menu before executing any of the
// possible event
let max_width = self.get_values().iter().fold(0, |acc, suggestion| {
let str_len = suggestion.value.len() + self.default_details.col_padding;
if str_len > acc {
str_len
} else {
acc
}
});

// If no default width is found, then the total screen width is used to estimate
// the column width based on the default number of columns
let default_width = if let Some(col_width) = self.default_details.col_width {
col_width
} else {
let col_width = painter.screen_width() / self.default_details.columns;
col_width as usize
};

// Adjusting the working width of the column based the max line width found
// in the menu values
if max_width > default_width {
self.working_details.col_width = max_width;
} else {
self.working_details.col_width = default_width;
};

// The working columns is adjusted based on possible number of columns
// that could be fitted in the screen with the calculated column width
let possible_cols = painter.screen_width() / self.working_details.col_width as u16;
if possible_cols > self.default_details.columns {
self.working_details.columns = self.default_details.columns.max(1);
} else {
self.working_details.columns = possible_cols;
}

// Updating the working rows to display the description
if self.menu_required_lines(painter.screen_width()) <= painter.remaining_lines() {
self.working_details.description_rows = self.default_details.description_rows;
self.show_examples = true;
} else {
self.working_details.description_rows = painter
.remaining_lines()
.saturating_sub(self.default_details.selection_rows + 1)
as usize;

self.show_examples = false;
}

match event {
MenuEvent::Activate(_) => {
self.reset_position();
Expand Down Expand Up @@ -578,6 +528,54 @@ impl Menu for DescriptionMenu {
}
MenuEvent::PreviousPage | MenuEvent::NextPage => {}
}

let max_width = self.get_values().iter().fold(0, |acc, suggestion| {
let str_len = suggestion.value.len() + self.default_details.col_padding;
if str_len > acc {
str_len
} else {
acc
}
});

// If no default width is found, then the total screen width is used to estimate
// the column width based on the default number of columns
let default_width = if let Some(col_width) = self.default_details.col_width {
col_width
} else {
let col_width = painter.screen_width() / self.default_details.columns;
col_width as usize
};

// Adjusting the working width of the column based the max line width found
// in the menu values
if max_width > default_width {
self.working_details.col_width = max_width;
} else {
self.working_details.col_width = default_width;
};

// The working columns is adjusted based on possible number of columns
// that could be fitted in the screen with the calculated column width
let possible_cols = painter.screen_width() / self.working_details.col_width as u16;
if possible_cols > self.default_details.columns {
self.working_details.columns = self.default_details.columns.max(1);
} else {
self.working_details.columns = possible_cols;
}

// Updating the working rows to display the description
if self.menu_required_lines(painter.screen_width()) <= painter.remaining_lines() {
self.working_details.description_rows = self.default_details.description_rows;
self.show_examples = true;
} else {
self.working_details.description_rows = painter
.remaining_lines()
.saturating_sub(self.default_details.selection_rows + 1)
as usize;

self.show_examples = false;
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/menu/ide_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ impl Menu for IdeMenu {
painter: &Painter,
) {
if let Some(event) = self.event.take() {
// The working value for the menu are updated first before executing any of the
match event {
MenuEvent::Activate(updated) => {
self.active = true;
Expand Down
Loading