Skip to content

Commit 8aa6cbe

Browse files
committed
Modify theme load logic so that now we don't need to reload GNOME-Shell
to change IBus themes. Signed-off-by: Hollow Man <hollowman@hollowman.ml>
1 parent 42bc81b commit 8aa6cbe

8 files changed

Lines changed: 44 additions & 128 deletions

File tree

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
(English version is down below)
1919

20-
### 注意:如果后期无重大 BUG 的话,v14 将是支持 GNOME 3.38 的最后一个版本
20+
### 注意:如果后期无重大 BUG 的话,v15 将是支持 GNOME 3.38 的最后一个版本
2121

2222
在 GNOME Shell 中更改 IBus 的候选框方向、shell 主题、背景图片、字体和输入法默认语言。
2323

@@ -53,7 +53,7 @@ IBus Tweaker 中提供的主题已经被我制作成为 IBus 主题样式表合
5353
### _提示:_
5454

5555
1. 推荐使用 X11。如果你在 Wayland 中更改 IBus 主题,你的当前所有工作都将会丢失,因为 Wayland 下只支持通过重新登陆来重启 GNOME-shell。
56-
2. 在 Fedora 33 和 Ubuntu 20.04, GNOME-shell 3.38 中(v3,v5,v9,v11,v14)通过了测试。
56+
2. 在 Fedora 33 和 Ubuntu 20.04, GNOME-shell 3.38 中(v3,v5,v9,v11,v14,v15)通过了测试。
5757
3. 对于那些不使用 GNOME 而是使用如 KDE,XFCE 等桌面环境的用户,更改 IBus GTK 主题也请使用我的另外一个项目[IBus-Theme](https://github.com/HollowMan6/IBus-Theme)
5858

5959
## 更改 IBus 背景图片的实现
@@ -78,20 +78,19 @@ background-size: cover;
7878
- [x] V9: 增加更改 IBus 背景图片功能。
7979
- [x] V14: 将从 GNOME-Shell 主题提取 IBus 样式功能剥离,使用 Python 下 CSS 解析器而并非正则表达式实现功能,生成额外 IBus 样式表供用户修改测试使用。
8080
- [x] V14: 本扩展改为接受用户提供的 IBus 样式表进行样式的应用。
81+
- [x] V15: 修改主题加载逻辑,免去每次更换主题都要重启 GNOME-Shell。
8182

8283
## 致谢
8384

8485
1. [ibus-font-setting](https://extensions.gnome.org/extension/1121/ibus-font-setting/)
8586
2. [ibus-tweaker](https://github.com/tuberry/ibus-tweaker)
86-
3. [user-theme](https://gitlab.gnome.org/GNOME/gnome-shell-extensions/-/tree/master/extensions/user-theme)
87-
4. [shell-restarter](https://github.com/koolskateguy89/gnome-shell-extension-shell-restarter)
88-
5. [background-logo](https://pagure.io/background-logo-extension)
87+
3. [background-logo](https://pagure.io/background-logo-extension)
8988

9089
_该项目是谷歌编程之夏 (GSoC) 2021 于[OpenSUSE](https://github.com/openSUSE/mentoring/issues/158)社区成果的一部分。_
9190

9291
# Customize IBus
9392

94-
### Note: If no severe bug was found later, v14 will be the last version that support GNOME 3.38.
93+
### Note: If no severe bug was found later, v15 will be the last version that support GNOME 3.38.
9594

9695
Customize IBus for orientation, shell theme, background picture, font and ascii mode auto-switch.
9796

@@ -125,7 +124,7 @@ Themes in IBus Tweaker have been converted by me as IBus theme stylesheets colle
125124
### _NOTE:_
126125

127126
1. Recommend to use X11. If you change IME theme under Wayland, all your current work may be lost (Since Wayland only support relogin to restart the GNOME-shell).
128-
2. Tested on Fedora 33 and Ubuntu 20.04, GNOME-shell 3.38(v3, v5, v9, v11, v14).
127+
2. Tested on Fedora 33 and Ubuntu 20.04, GNOME-shell 3.38(v3, v5, v9, v11, v14, v15).
129128
3. For users who don't use GNOME but other desktop environments like KDE, XFCE, etc., please also use another project of mine [IBus-Theme](https://github.com/HollowMan6/IBus-Theme) to use a different GTK theme for IBus.
130129

131130
## To-do
@@ -136,14 +135,13 @@ Themes in IBus Tweaker have been converted by me as IBus theme stylesheets colle
136135
- [x] v9: Add functionality to modify IBus Background picture.
137136
- [x] V14: Strip the current function of extracting IBus style from Gnome shell theme, implement using CSS parser libraries in Python instead of regular expression. Additional IBus style sheets are generated for users to modify and test.
138137
- [x] V14: Modify this extension to accept IBus style sheets provided by users for style application.
138+
- [x] V15: Modify theme load logic so that now we don't need to reload GNOME-Shell to change IBus themes.
139139

140140
## Acknowledgements
141141

142142
1. [ibus-font-setting](https://extensions.gnome.org/extension/1121/ibus-font-setting/)
143143
2. [ibus-tweaker](https://github.com/tuberry/ibus-tweaker)
144-
3. [user-theme](https://gitlab.gnome.org/GNOME/gnome-shell-extensions/-/tree/master/extensions/user-theme)
145-
4. [shell-restarter](https://github.com/koolskateguy89/gnome-shell-extension-shell-restarter)
146-
5. [background-logo](https://pagure.io/background-logo-extension)
144+
3. [background-logo](https://pagure.io/background-logo-extension)
147145

148146
_This project is part of the achievement of the Google Summer of Code 2021 at [OpenSUSE](https://github.com/openSUSE/mentoring/issues/158)._
149147

customize-ibus@hollowman.ml/extension.js

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const UNKNOWN = { ON: 0, OFF: 1, DEFAULT: 2 };
3636
const ASCIIMODES = ["en", "A", "英"];
3737
const INPUTMODE = "InputMode";
3838
const uuid = "customize-ibus@hollowman.ml";
39-
const SETTINGS_KEY = "custom-theme";
40-
const PERMISSIONS_MODE = 0o744;
4139

4240
const IBusAutoSwitch = GObject.registerClass(
4341
{
@@ -280,104 +278,43 @@ const IBusThemeManager = GObject.registerClass(
280278
class IBusThemeManager extends GObject.Object {
281279
_init() {
282280
super._init();
283-
this._settings = ExtensionUtils.getSettings();
284281
this._prevCssStylesheet = null;
285282
this.enable();
286283
}
287284

288285
enable() {
289-
this._changedId = this._settings.connect(
290-
`changed::${SETTINGS_KEY}`,
286+
this._changedId = gsettings.connect(
287+
`changed::${Fields.CUSTOMTHEME}`,
291288
this._changeTheme.bind(this)
292289
);
293290
this._changeTheme();
294291
}
295292

296293
disable() {
297294
if (this._changedId) {
298-
this._settings.disconnect(this._changedId);
295+
gsettings.disconnect(this._changedId);
299296
this._changedId = 0;
300297
}
301298
this._changeTheme();
302299
}
303300

304301
// Load stylesheet
305302
_changeTheme() {
306-
let stylesheet = this._settings.get_string(SETTINGS_KEY);
307-
let enabled = this._settings.get_boolean(Fields.ENABLECUSTOMTHEME);
303+
let stylesheet = gsettings.get_string(Fields.CUSTOMTHEME);
304+
let enabled = gsettings.get_boolean(Fields.ENABLECUSTOMTHEME);
308305

309-
let newFileContent = "";
310-
let notFirstStart = false;
311-
let needRestart = false;
312-
if (this._prevCssStylesheet) notFirstStart = true;
306+
let themeContext = St.ThemeContext.get_for_stage(global.stage);
307+
let theme = themeContext.get_theme();
308+
if (this._prevCssStylesheet)
309+
theme.unload_stylesheet(Gio.File.new_for_path(this._prevCssStylesheet));
313310
if (stylesheet && enabled) {
314311
global.log(_("loading user theme for IBus:") + stylesheet);
315-
let file = Gio.File.new_for_path(stylesheet);
316-
let [success, contents] = file.load_contents(null);
317-
global.log(success);
318-
newFileContent =
319-
"/* " +
320-
_("Copied from Source File: ") +
321-
stylesheet +
322-
" */\n\n" +
323-
contents;
312+
theme.load_stylesheet(Gio.File.new_for_path(stylesheet));
324313
this._prevCssStylesheet = stylesheet;
325314
} else {
326315
global.log(_("loading default theme for IBus"));
327-
if (stylesheet)
328-
this._settings.set_value(SETTINGS_KEY, new GLib.Variant("s", ""));
329-
this._prevCssStylesheet = "Unsetted";
316+
this._prevCssStylesheet = "";
330317
}
331-
let file = Gio.File.new_for_path(
332-
GLib.build_filenamev([
333-
global.userdatadir,
334-
"extensions",
335-
uuid,
336-
"stylesheet.css",
337-
])
338-
);
339-
if (!file.query_exists(null))
340-
file = Gio.File.new_for_path(
341-
GLib.build_filenamev([
342-
global.datadir,
343-
"extensions",
344-
uuid,
345-
"stylesheet.css",
346-
])
347-
);
348-
if (
349-
GLib.mkdir_with_parents(
350-
file.get_parent().get_path(),
351-
PERMISSIONS_MODE
352-
) === 0
353-
) {
354-
let [success, contents] = file.load_contents(null);
355-
if (success) {
356-
if (contents != newFileContent && stylesheet != file.get_path()) {
357-
file.replace_contents(
358-
newFileContent,
359-
null,
360-
false,
361-
Gio.FileCreateFlags.REPLACE_DESTINATION,
362-
null
363-
);
364-
needRestart = true;
365-
}
366-
} else {
367-
file.replace_contents(
368-
newFileContent,
369-
null,
370-
false,
371-
Gio.FileCreateFlags.REPLACE_DESTINATION,
372-
null
373-
);
374-
}
375-
}
376-
if (notFirstStart || needRestart) this.restart();
377-
}
378-
379-
restart() {
380-
Meta.restart(_("Restarting..."));
381318
}
382319
}
383320
);

customize-ibus@hollowman.ml/locale/customize-ibus.pot

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#, fuzzy
77
msgid ""
88
msgstr ""
9-
"Project-Id-Version: Customize Ibus 14\n"
9+
"Project-Id-Version: Customize Ibus 15\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2021-04-19 19:15+0800\n"
11+
"POT-Creation-Date: 2021-04-20 12:59+0800\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,26 +17,18 @@ msgstr ""
1717
"Content-Type: text/plain; charset=CHARSET\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: extension.js:213
20+
#: extension.js:211
2121
msgid "loading background for IBus:"
2222
msgstr ""
2323

24-
#: extension.js:314
24+
#: extension.js:311
2525
msgid "loading user theme for IBus:"
2626
msgstr ""
2727

28-
#: extension.js:320
29-
msgid "Copied from Source File: "
30-
msgstr ""
31-
32-
#: extension.js:326
28+
#: extension.js:315
3329
msgid "loading default theme for IBus"
3430
msgstr ""
3531

36-
#: extension.js:380
37-
msgid "Restarting..."
38-
msgstr ""
39-
4032
#: prefs.js:61
4133
msgid "Custom IME theme"
4234
msgstr ""

customize-ibus@hollowman.ml/locale/zh_CN/LC_MESSAGES/customize-ibus.po

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: customize-ibus 1\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2021-04-19 19:15+0800\n"
11+
"POT-Creation-Date: 2021-04-20 12:52+0800\n"
1212
"PO-Revision-Date: 2021-03-27 16:22+0800\n"
1313
"Last-Translator: Hollow Man <hollowman@hollowman.ml>\n"
1414
"Language-Team: Chinese (simplified) <i18n-zh@googlegroups.com>\n"
@@ -17,26 +17,18 @@ msgstr ""
1717
"Content-Type: text/plain; charset=UTF-8\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: extension.js:213
20+
#: extension.js:211
2121
msgid "loading background for IBus:"
2222
msgstr "正在为IBus加载背景:"
2323

24-
#: extension.js:314
24+
#: extension.js:311
2525
msgid "loading user theme for IBus:"
2626
msgstr "正在为IBus加载用户主题:"
2727

28-
#: extension.js:320
29-
msgid "Copied from Source File: "
30-
msgstr "拷贝自源文件:"
31-
32-
#: extension.js:326
28+
#: extension.js:315
3329
msgid "loading default theme for IBus"
3430
msgstr "正在为IBus加载当前系统主题"
3531

36-
#: extension.js:380
37-
msgid "Restarting..."
38-
msgstr "正在重新启动⋯⋯"
39-
4032
#: prefs.js:61
4133
msgid "Custom IME theme"
4234
msgstr "自定义主题"

customize-ibus@hollowman.ml/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"original-authors": ["hollowman@hollowman.ml"],
99
"uuid": "customize-ibus@hollowman.ml",
1010
"extension-id": "customize-ibus",
11-
"version": 14
11+
"version": 15
1212
}

customize-ibus@hollowman.ml/prefs.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const CustomizeIBus = GObject.registerClass(
8585
});
8686
this._fileChooser.connect("response", (dlg, response) => {
8787
if (response !== Gtk.ResponseType.ACCEPT) return;
88-
gsettings.set_string("custom-bg", dlg.get_file().get_path());
88+
gsettings.set_string(Fields.CUSTOMBG, dlg.get_file().get_path());
8989
});
9090

9191
this._logoPicker = new Gtk.Button({
@@ -96,7 +96,7 @@ const CustomizeIBus = GObject.registerClass(
9696
this._fileChooser.show();
9797
});
9898
gsettings.connect(
99-
"changed::custom-bg",
99+
`changed::${Fields.CUSTOMBG}`,
100100
this._updateLogoPicker.bind(this)
101101
);
102102
this._updateLogoPicker();
@@ -110,7 +110,7 @@ const CustomizeIBus = GObject.registerClass(
110110
});
111111
this._cssFileChooser.connect("response", (dlg, response) => {
112112
if (response !== Gtk.ResponseType.ACCEPT) return;
113-
gsettings.set_string("custom-theme", dlg.get_file().get_path());
113+
gsettings.set_string(Fields.CUSTOMTHEME, dlg.get_file().get_path());
114114
});
115115

116116
this._cssPicker = new Gtk.Button({
@@ -121,20 +121,20 @@ const CustomizeIBus = GObject.registerClass(
121121
this._cssFileChooser.show();
122122
});
123123
gsettings.connect(
124-
"changed::custom-theme",
124+
`changed::${Fields.CUSTOMTHEME}`,
125125
this._updateCssPicker.bind(this)
126126
);
127127
this._updateCssPicker();
128128
}
129129

130130
_updateLogoPicker() {
131-
const filename = gsettings.get_string("custom-bg");
131+
const filename = gsettings.get_string(Fields.CUSTOMBG);
132132
if (!GLib.basename(filename)) this._logoPicker.label = _("(None)");
133133
else this._logoPicker.label = GLib.basename(filename);
134134
}
135135

136136
_updateCssPicker() {
137-
const filename = gsettings.get_string("custom-theme");
137+
const filename = gsettings.get_string(Fields.CUSTOMTHEME);
138138
if (!GLib.basename(filename)) this._cssPicker.label = _("(None)");
139139
else this._cssPicker.label = GLib.basename(filename);
140140
}
@@ -163,18 +163,18 @@ const CustomizeIBus = GObject.registerClass(
163163
});
164164
this._field_use_custom_font.connect("notify::active", (widget) => {
165165
this._field_custom_font.set_sensitive(widget.active);
166-
ibusGsettings.set_boolean("use-custom-font", widget.active);
166+
ibusGsettings.set_boolean(Fields.USECUSTOMFONT, widget.active);
167167
});
168168
this._field_use_custom_bg.connect("notify::active", (widget) => {
169169
this._logoPicker.set_sensitive(widget.active);
170-
ibusGsettings.set_boolean("use-custom-bg", widget.active);
170+
ibusGsettings.set_boolean(Fields.USECUSTOMBG, widget.active);
171171
});
172172
this._field_enable_custom_theme.connect("notify::active", (widget) => {
173173
this._cssPicker.set_sensitive(widget.active);
174-
ibusGsettings.set_boolean("enable-custom-theme", widget.active);
174+
ibusGsettings.set_boolean(Fields.ENABLECUSTOMTHEME, widget.active);
175175
});
176176
this._field_custom_font.connect("font-set", (widget) => {
177-
ibusGsettings.set_string("custom-font", widget.font_name);
177+
ibusGsettings.set_string(Fields.CUSTOMFONT, widget.font_name);
178178
gsettings.set_string(Fields.CUSTOMFONT, widget.font_name);
179179
});
180180

customize-ibus@hollowman.ml/stylesheet.css

Lines changed: 0 additions & 3 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "customize-ibus",
3-
"version": "14.0.0",
3+
"version": "15.0.0",
44
"description": "Customize IBus for orientation, shell theme, background picture, font and ascii mode auto-switch.",
55
"dependencies": {
6-
"eslint": "^7.21.0",
7-
"eslint-config-prettier": "^8.1.0",
8-
"eslint-plugin-prettier": "^3.3.1",
9-
"husky": "^5.1.3",
6+
"eslint": "^7.24.0",
7+
"eslint-config-prettier": "^8.2.0",
8+
"eslint-plugin-prettier": "^3.4.0",
9+
"husky": "^6.0.0",
1010
"lint-staged": "^10.5.4",
1111
"prettier": "^2.2.1"
1212
},

0 commit comments

Comments
 (0)