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

Revert "Gtk4Prep - Handle restoring and syncing window state in App #822

Merged
merged 2 commits into from
Dec 15, 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
23 changes: 9 additions & 14 deletions data/io.elementary.terminal.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
</enum>

<schema path="/io/elementary/terminal/saved-state/" id="io.elementary.terminal.saved-state">
<key name="window-height" type="i">
<default>700</default>
<summary>Most recent window height</summary>
<description>Most recent window height</description>
</key>
<key name="window-width" type="i">
<default>1024</default>
<summary>Most recent window width</summary>
<description>Most recent window width</description>
</key>
<key name="is-maximized" type="b">
<default>false</default>
<summary>Whether window is maximized</summary>
<description>Whether the main application window is maximized or not</description>
<key name="window-size" type="(ii)">
<default>(-1, -1)</default>
<summary>Most recent window size</summary>
<description>Most recent window size (width, height)</description>
</key>
<key name="window-state" enum="pantheon-terminal-window-states">
<default>"Normal"</default>
<summary>The saved state of the window.</summary>
<description>The saved state of the window.</description>
</key>
<key name="tabs" type="as">
<default>[]</default>
Expand Down
30 changes: 2 additions & 28 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ public class Terminal.Application : Gtk.Application {
var new_window = new MainWindow (this, active_window == null);
new_window.present ();
new_window.set_size_request (
saved_state.get_int ("window-width"),
saved_state.get_int ("window-height")
active_window.width_request,
active_window.height_request
);

new_window.add_tab_with_working_directory (dir);
Expand Down Expand Up @@ -326,10 +326,6 @@ public class Terminal.Application : Gtk.Application {
window.add_tab_with_working_directory (working_directory, null, new_tab);
}

//TODO In Gtk4 we can just bind the settings to first window properties
// instead of this function
restore_saved_state (window, is_first_window);

if (options.lookup ("minimized", "b", out minimized) && minimized) {
window.iconify ();
} else {
Expand All @@ -338,28 +334,6 @@ public class Terminal.Application : Gtk.Application {
return 0;
}

private void restore_saved_state (Gtk.Window window, bool is_first_window) {
window.resize (
Application.saved_state.get_int ("window-width"),
Application.saved_state.get_int ("window-height")
);

if (Application.saved_state.get_boolean ("is-maximized")) {
window.maximize ();
}

if (is_first_window) {
window.size_allocate.connect ((alloc) => {
if (!window.is_maximized) {
Application.saved_state.set_int ("window-width", alloc.width);
Application.saved_state.set_int ("window-height", alloc.height);
}

Application.saved_state.set_boolean ("is-maximized", window.is_maximized);
});
}
}

protected override void dbus_unregister (DBusConnection connection, string path) {
if (dbus_id != 0) {
connection.unregister_object (dbus_id);
Expand Down
55 changes: 55 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Terminal {
}

private Gtk.EventControllerKey key_controller;
private uint timer_window_state_change = 0;
private uint focus_timeout = 0;

private const int NORMAL = 0;
Expand Down Expand Up @@ -247,6 +248,7 @@ namespace Terminal {

set_size_request (Application.MINIMUM_WIDTH, Application.MINIMUM_HEIGHT);

restore_saved_state ();
show_all ();

if (recreate_tabs) {
Expand Down Expand Up @@ -489,6 +491,28 @@ namespace Terminal {
return false;
}

private void restore_saved_state () {
var rect = Gdk.Rectangle ();
Terminal.Application.saved_state.get ("window-size", "(ii)", out rect.width, out rect.height);

default_width = rect.width;
default_height = rect.height;

if (default_width == -1 || default_height == -1) {
var geometry = get_display ().get_primary_monitor ().get_geometry ();

default_width = geometry.width * 2 / 3;
default_height = geometry.height * 3 / 4;
}

var window_state = Terminal.Application.saved_state.get_enum ("window-state");
if (window_state == MainWindow.MAXIMIZED) {
maximize ();
} else if (window_state == MainWindow.FULLSCREEN) {
is_fullscreen = true;
}
}

private void on_tab_added (Hdy.TabPage tab, int pos) {
var term = get_term_widget (tab);
term.main_window = this;
Expand Down Expand Up @@ -601,6 +625,37 @@ namespace Terminal {
return appinfo;
}

protected override bool configure_event (Gdk.EventConfigure event) {
// triggered when the size, position or stacking of the window has changed
// it is delayed 400ms to prevent spamming gsettings
if (timer_window_state_change > 0) {
GLib.Source.remove (timer_window_state_change);
}

timer_window_state_change = GLib.Timeout.add (400, () => {
timer_window_state_change = 0;
if (get_window () == null)
return false;

/* Check for fullscreen first: https://github.com/elementary/terminal/issues/377 */
if ((get_window ().get_state () & Gdk.WindowState.FULLSCREEN) != 0) {
Terminal.Application.saved_state.set_enum ("window-state", MainWindow.FULLSCREEN);
} else if (is_maximized) {
Terminal.Application.saved_state.set_enum ("window-state", MainWindow.MAXIMIZED);
} else {
Terminal.Application.saved_state.set_enum ("window-state", MainWindow.NORMAL);

var rect = Gdk.Rectangle ();
get_size (out rect.width, out rect.height);
Terminal.Application.saved_state.set ("window-size", "(ii)", rect.width, rect.height);
}

return false;
});

return base.configure_event (event);
}

private void open_tabs () {
string[] tabs = {};
double[] zooms = {};
Expand Down
Loading