Skip to content

Commit

Permalink
Port to GNOME 45
Browse files Browse the repository at this point in the history
  • Loading branch information
MateusRodCosta committed Sep 24, 2023
1 parent b3417c2 commit f9723ca
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

A gnome-shell extension that switches to the specified workspace on login.

Supports GNOME 40+.
Supports GNOME 45+.
76 changes: 30 additions & 46 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,42 @@
// Uses code from No overview extension and Workspace Indicator
// Contributors: @MateusRodCost, @fthx, @fmuellner

const Gio = imports.gi.Gio;
const Main = imports.ui.main;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
import {layoutManager} from 'resource:///org/gnome/shell/ui/main.js';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';

const DEFAULT_WORKSPACE_SCHEMA = 'org.gnome.shell.extensions.default-workspace';
const DEFAULT_WORKSPACE_KEY = 'default-workspace-number';

class Extension {
export default class DefaultWorkspaceExtension extends Extension {
enable() {
this._settings = this.getSettings();
let defaultWorkspace = this._settings.get_int(DEFAULT_WORKSPACE_KEY);

constructor() {
this.handlerStartup = null;
if (!layoutManager._startingUp) {
return;
}

enable() {
this.settings = ExtensionUtils.getSettings(DEFAULT_WORKSPACE_SCHEMA);
// The following is based on code from No overview at startup extension
// That will guarantee gnome-shell doesn't crash
if (!Main.layoutManager._startingUp) {
return;
}
this.handlerStartup = Main.layoutManager.connect('startup-complete', () => {
let ws = this.settings.get_int(DEFAULT_WORKSPACE_KEY);
this._changeWorkspace(ws -1);
});
}
this._startupHandler = layoutManager.connect('startup-complete', () => {
this._changeWorkspace(defaultWorkspace);
});
}

disable() {
if(this.handlerStartup) {
Main.layoutManager.disconnect(this.handlerStartup);
this.handlerStartup = null;
}
this.settings = null;
disable() {
if (this._startupHandler) {
layoutManager.disconnect(this.handlerStartup);
this._startupHandler = null;
}

// This is based on code from Workspace Indicator
// This will guarantee the workspace in the most correct way
_changeWorkspace(index) {
let workspaceManager = global.workspace_manager;

if (index >= 0 && index < workspaceManager.n_workspaces) {
let metaWorkspace = workspaceManager.get_workspace_by_index(index);
if(metaWorkspace) {
metaWorkspace.activate(global.get_current_time());
}
} else {
log('Unable to switch to a invalid workspace')
}

this._settings = null;
}

_changeWorkspace(index) {
let workspaceManager = global.workspace_manager;
let goalWorkspace = workspaceManager.get_workspace_by_index(index - 1);

if(goalWorkspace){
goalWorkspace.activate(global.get_current_time());
console.info(`${this.uuid}: Switched to workspace ${index} on login`);
} else {
console.warn(`${this.uuid}: Unable to switch to a invalid workspace (chosen workspace was ${index})`);
}
}

function init() {
return new Extension();
}
}
}
6 changes: 4 additions & 2 deletions src/metadata.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "Default Workspace",
"description": "Switches to the specified workspace on login.\nUseful for fixed number of workspace setups where the first workspace isn't the main one.",
"shell-version": ["40", "41", "42", "43", "44"],
"shell-version": ["45"],
"url": "https://github.com/MateusRodCosta/gnome-shell-extension-default-workspace",
"uuid": "default-workspace@mateusrodcosta.com",
"version": 3
"settings-schema": "org.gnome.shell.extensions.default-workspace",
"version": 5

}
56 changes: 19 additions & 37 deletions src/prefs.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
const {GObject, Gtk, Gio} = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
import Gio from 'gi://Gio';
import Gtk from 'gi://Gtk';
import {ExtensionPreferences} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

const DEFAULT_WORKSPACE_SCHEMA = 'org.gnome.shell.extensions.default-workspace';
const DEFAULT_WORKSPACE_KEY = 'default-workspace-number';

const DefaultWorkspaceBuilderScope = GObject.registerClass({
Implements: [Gtk.BuilderScope],
}, class DefaultWorkspaceBuilderScope extends GObject.Object {
export default class DefaultWorkspacePreferences extends ExtensionPreferences {

vfunc_create_closure(builder, handlerName, flags, connectObject) {
if (flags & Gtk.BuilderClosureFlags.SWAPPED)
throw new Error('Unsupported template signal flag "swapped"');
fillPreferencesWindow(window) {
let settings = this.getSettings();

if (typeof this[handlerName] === 'undefined')
throw new Error(`${handlerName} is undefined`);
let builder = new Gtk.Builder();

return this[handlerName].bind(connectObject || this);
}

});

function init () {}

function buildPrefsWidget () {
builder.add_from_file(`${this.path}/ui/prefs.ui`);

this.settings = ExtensionUtils.getSettings(DEFAULT_WORKSPACE_SCHEMA);
let spin_row = builder.get_object('default_workspace_spinrow');
if (spin_row) {
settings.bind(
DEFAULT_WORKSPACE_KEY,
spin_row,
'value',
Gio.SettingsBindFlags.DEFAULT
);
}

let builder = new Gtk.Builder();

builder.set_scope(new DefaultWorkspaceBuilderScope());
builder.set_translation_domain('gettext-domain');
builder.add_from_file(Me.dir.get_path() + '/ui/prefs.ui');

let sb = builder.get_object('default_workspace_spinbutton');
if(sb) {
this.settings.bind(
DEFAULT_WORKSPACE_KEY,
sb,
'value',
Gio.SettingsBindFlags.DEFAULT
);
let page = builder.get_object('preferences_main_page');
window.add(page);
}

return builder.get_object('box');
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

<key type="i" name="default-workspace-number">
<default>2</default>
<summary>The default workspace</summary>
<description>The number of the desired default workspace</description>
<summary>The number of chosen default workspace</summary>
<description>The default workspace is 2, as that is the minimum you have with dynamic workspaces.</description>
</key>

</schema>
Expand Down
24 changes: 24 additions & 0 deletions src/ui/DefaultWorkspace.cmb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE cambalache-project SYSTEM "cambalache-project.dtd">
<cambalache-project version="0.13.1" target_tk="gtk-4.0">
<ui>
(1,None,"defaultworkspace.ui","defaultworkspace.ui",None,None,None,None,None,None,None)
</ui>
<object>
(1,1,"AdwPreferencesPage","preferences_main_page",None,None,None,None,-1,None),
(1,2,"AdwPreferencesGroup","workspace_group",1,None,None,None,-1,None),
(1,3,"AdwSpinRow","default_workspace_spinrow",2,None,None,None,-1,None),
(1,4,"GtkAdjustment","default_workspace_adjustment",3,None,None,None,-1,None)
</object>
<object_property>
(1,2,"AdwPreferencesGroup","title","Workspace",None,None,None,None,None,None,None,None,None),
(1,3,"AdwPreferencesRow","title","Default Workspace",None,None,None,None,None,None,None,None,None),
(1,3,"AdwSpinRow","adjustment",None,None,None,None,None,4,None,None,None,None),
(1,4,"GtkAdjustment","lower","1.0",None,None,None,None,None,None,None,None,None),
(1,4,"GtkAdjustment","page-increment","1.0",None,None,None,None,None,None,None,None,None),
(1,4,"GtkAdjustment","page-size","1.0",None,None,None,None,None,None,None,None,None),
(1,4,"GtkAdjustment","step-increment","1.0",None,None,None,None,None,None,None,None,None),
(1,4,"GtkAdjustment","upper","37.0",None,None,None,None,None,None,None,None,None),
(1,4,"GtkAdjustment","value","1.0",None,None,None,None,None,None,None,None,None)
</object_property>
</cambalache-project>
63 changes: 18 additions & 45 deletions src/ui/prefs.ui
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
<?xml version='1.0' encoding='UTF-8'?>
<interface domain="my-gettext-domain">
<!-- Created with Cambalache 0.16.0 -->
<interface>
<!-- interface-name defaultworkspace.ui -->
<requires lib="gtk" version="4.0"/>
<object class="GtkBox" id="box">
<property name="orientation">vertical</property>
<property name="halign">center</property>
<property name="spacing">12</property>
<property name="margin_top">36</property>
<property name="margin_bottom">36</property>
<property name="margin_start">36</property>
<property name="margin_end">36</property>
<requires lib="libadwaita" version="1.4"/>
<object class="AdwPreferencesPage" id="preferences_main_page">
<child>
<object class="GtkFrame" id="frame">
<object class="AdwPreferencesGroup" id="workspace_group">
<property name="title">Workspace</property>
<child>
<object class="GtkListBox" id="list_box">
<property name="selection-mode">none</property>
<property name="show_separators">true</property>
<child>
<object class="GtkListBoxRow" id="default_workspace_row">
<child>
<object class="GtkBox" id="default_workspace_box">
<property name="spacing">12</property>
<property name="margin_top">6</property>
<property name="margin_bottom">6</property>
<property name="margin_start">6</property>
<property name="margin_end">6</property>
<child>
<object class="GtkLabel" id="default_workspace_label">
<property name="label">Default Workspace</property>
<property name="hexpand">true</property>
<property name="xalign">0</property>
</object>
</child>
<child>
<object class="GtkSpinButton" id="default_workspace_spinbutton">
<property name="adjustment">default_workspace_adjustment</property>
</object>
</child>
</object>
</child>
<object class="AdwSpinRow" id="default_workspace_spinrow">
<property name="adjustment">
<object class="GtkAdjustment" id="default_workspace_adjustment">
<property name="lower">1.0</property>
<property name="page-increment">1.0</property>
<property name="page-size">1.0</property>
<property name="step-increment">1.0</property>
<property name="upper">37.0</property>
<property name="value">1.0</property>
</object>
</child>
</property>
<property name="title">Default Workspace</property>
</object>
</child>
</object>
</child>
</object>
<object class="GtkAdjustment" id="default_workspace_adjustment">
<property name="lower">1.0</property>
<property name="page-increment">1.0</property>
<property name="page-size">1.0</property>
<property name="step-increment">1.0</property>
<property name="upper">37.0</property>
<property name="value">1.0</property>
</object>
</interface>

0 comments on commit f9723ca

Please sign in to comment.