Skip to content

Commit

Permalink
Listen to VimResized event and resize the window when needed
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
jacobmischka committed Jul 15, 2021
1 parent c036492 commit 142de5c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,23 @@ impl State {
}
}

pub fn should_resize(&self, columns: usize, rows: usize) -> bool {
columns != self.resize_request.1 as usize || rows != self.resize_request.0 as usize
}

pub fn calc_window_size(&self, columns: usize, rows: usize) -> (usize, usize) {
let &CellMetrics {
line_height,
char_width,
..
} = self.render_state.borrow().font_ctx.cell_metrics();

(
(columns as f64 * char_width).ceil() as usize,
(rows as f64 * line_height).ceil() as usize,
)
}

fn calc_nvim_size(&self) -> (usize, usize) {
let &CellMetrics {
line_height,
Expand Down Expand Up @@ -1500,7 +1517,9 @@ impl State {
}

pub fn option_set(&mut self, name: String, val: Value) -> RepaintMode {
if let "guifont" = name.as_str() { self.set_font_from_value(val) };
if let "guifont" = name.as_str() {
self.set_font_from_value(val)
};
RepaintMode::Nothing
}

Expand All @@ -1513,7 +1532,8 @@ impl State {
for font in &fonts {
let desc = FontDescription::from_string(&font);
if desc.get_size() > 0
&& exists_fonts.contains(&desc.get_family().unwrap_or_else(|| "".into()))
&& exists_fonts
.contains(&desc.get_family().unwrap_or_else(|| "".into()))
{
self.set_font_rpc(font);
return;
Expand Down
35 changes: 34 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ impl Ui {
move |args| set_completeopts(&*shell_ref, args),
);

let comps_ref = self.comps.clone();
let shell_ref = self.shell.clone();
let update_size = shell.state.borrow().subscribe(
SubscriptionKey::from("VimResized"),
&["&lines", "&columns"],
move |args| update_window_size(&*comps_ref, &*shell_ref, args),
);

let comps_ref = self.comps.clone();
let shell_ref = self.shell.clone();
window.connect_delete_event(move |_, _| gtk_delete(&*comps_ref, &*shell_ref));
Expand Down Expand Up @@ -279,6 +287,7 @@ impl Ui {
&update_title,
&update_subtitle,
&update_completeopt,
&update_size,
);
}));

Expand All @@ -300,6 +309,7 @@ impl Ui {
update_title: &SubscriptionHandle,
update_subtitle: &Option<SubscriptionHandle>,
update_completeopt: &SubscriptionHandle,
update_size: &SubscriptionHandle,
) {
plug_manager
.borrow_mut()
Expand All @@ -308,6 +318,7 @@ impl Ui {
shell.set_autocmds();
shell.run_now(&update_title);
shell.run_now(&update_completeopt);
shell.run_now(&update_size);
if let Some(ref update_subtitle) = update_subtitle {
shell.run_now(&update_subtitle);
}
Expand Down Expand Up @@ -471,7 +482,9 @@ fn on_help_about(window: &gtk::ApplicationWindow) {
let about = AboutDialog::new();
about.set_transient_for(Some(window));
about.set_program_name("NeovimGtk");
about.set_version(Some(crate::GIT_BUILD_VERSION.unwrap_or(env!("CARGO_PKG_VERSION"))));
about.set_version(Some(
crate::GIT_BUILD_VERSION.unwrap_or(env!("CARGO_PKG_VERSION")),
));
about.set_logo_icon_name(Some("org.daa.NeovimGtk"));
about.set_authors(&[env!("CARGO_PKG_AUTHORS")]);
about.set_comments(Some(misc::about_comments().as_str()));
Expand Down Expand Up @@ -544,6 +557,26 @@ fn update_window_title(comps: &Arc<UiMutex<Components>>, args: Vec<String>) {
window.set_title(filename);
}

fn update_window_size(comps: &UiMutex<Components>, shell: &RefCell<Shell>, args: Vec<String>) {
let comps_ref = comps.clone();
let comps = comps_ref.borrow();
let window = comps.window.as_ref().unwrap();

let lines = &args[0];
let cols = &args[1];

if let (Ok(lines), Ok(cols)) = (lines.parse::<usize>(), cols.parse::<usize>()) {
let state_ref = shell.borrow().state.clone();
let state = state_ref.borrow();

if state.should_resize(cols, lines) {
let (width, height) = state.calc_window_size(cols, lines);

window.resize(width as i32, height as i32);
}
}
}

#[derive(Serialize, Deserialize)]
struct WindowState {
current_width: i32,
Expand Down

0 comments on commit 142de5c

Please sign in to comment.