Skip to content

Commit

Permalink
Modify theme load logic so that now we don't need to reload GNOME-Shell
Browse files Browse the repository at this point in the history
to change IBus themes.

Signed-off-by: Hollow Man <[email protected]>
  • Loading branch information
HollowMan6 committed Apr 20, 2021
1 parent 42bc81b commit 8aa6cbe
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 128 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

(English version is down below)

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

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

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

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

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

## 致谢

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

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

# Customize IBus

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

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

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

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).
2. Tested on Fedora 33 and Ubuntu 20.04, GNOME-shell 3.38(v3, v5, v9, v11, v14).
2. Tested on Fedora 33 and Ubuntu 20.04, GNOME-shell 3.38(v3, v5, v9, v11, v14, v15).
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.

## To-do
Expand All @@ -136,14 +135,13 @@ Themes in IBus Tweaker have been converted by me as IBus theme stylesheets colle
- [x] v9: Add functionality to modify IBus Background picture.
- [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.
- [x] V14: Modify this extension to accept IBus style sheets provided by users for style application.
- [x] V15: Modify theme load logic so that now we don't need to reload GNOME-Shell to change IBus themes.

## Acknowledgements

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

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

Expand Down
85 changes: 11 additions & 74 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ const UNKNOWN = { ON: 0, OFF: 1, DEFAULT: 2 };
const ASCIIMODES = ["en", "A", "英"];
const INPUTMODE = "InputMode";
const uuid = "[email protected]";
const SETTINGS_KEY = "custom-theme";
const PERMISSIONS_MODE = 0o744;

const IBusAutoSwitch = GObject.registerClass(
{
Expand Down Expand Up @@ -280,104 +278,43 @@ const IBusThemeManager = GObject.registerClass(
class IBusThemeManager extends GObject.Object {
_init() {
super._init();
this._settings = ExtensionUtils.getSettings();
this._prevCssStylesheet = null;
this.enable();
}

enable() {
this._changedId = this._settings.connect(
`changed::${SETTINGS_KEY}`,
this._changedId = gsettings.connect(
`changed::${Fields.CUSTOMTHEME}`,
this._changeTheme.bind(this)
);
this._changeTheme();
}

disable() {
if (this._changedId) {
this._settings.disconnect(this._changedId);
gsettings.disconnect(this._changedId);
this._changedId = 0;
}
this._changeTheme();
}

// Load stylesheet
_changeTheme() {
let stylesheet = this._settings.get_string(SETTINGS_KEY);
let enabled = this._settings.get_boolean(Fields.ENABLECUSTOMTHEME);
let stylesheet = gsettings.get_string(Fields.CUSTOMTHEME);
let enabled = gsettings.get_boolean(Fields.ENABLECUSTOMTHEME);

let newFileContent = "";
let notFirstStart = false;
let needRestart = false;
if (this._prevCssStylesheet) notFirstStart = true;
let themeContext = St.ThemeContext.get_for_stage(global.stage);
let theme = themeContext.get_theme();
if (this._prevCssStylesheet)
theme.unload_stylesheet(Gio.File.new_for_path(this._prevCssStylesheet));
if (stylesheet && enabled) {
global.log(_("loading user theme for IBus:") + stylesheet);
let file = Gio.File.new_for_path(stylesheet);
let [success, contents] = file.load_contents(null);
global.log(success);
newFileContent =
"/* " +
_("Copied from Source File: ") +
stylesheet +
" */\n\n" +
contents;
theme.load_stylesheet(Gio.File.new_for_path(stylesheet));
this._prevCssStylesheet = stylesheet;
} else {
global.log(_("loading default theme for IBus"));
if (stylesheet)
this._settings.set_value(SETTINGS_KEY, new GLib.Variant("s", ""));
this._prevCssStylesheet = "Unsetted";
this._prevCssStylesheet = "";
}
let file = Gio.File.new_for_path(
GLib.build_filenamev([
global.userdatadir,
"extensions",
uuid,
"stylesheet.css",
])
);
if (!file.query_exists(null))
file = Gio.File.new_for_path(
GLib.build_filenamev([
global.datadir,
"extensions",
uuid,
"stylesheet.css",
])
);
if (
GLib.mkdir_with_parents(
file.get_parent().get_path(),
PERMISSIONS_MODE
) === 0
) {
let [success, contents] = file.load_contents(null);
if (success) {
if (contents != newFileContent && stylesheet != file.get_path()) {
file.replace_contents(
newFileContent,
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null
);
needRestart = true;
}
} else {
file.replace_contents(
newFileContent,
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null
);
}
}
if (notFirstStart || needRestart) this.restart();
}

restart() {
Meta.restart(_("Restarting..."));
}
}
);
Expand Down
18 changes: 5 additions & 13 deletions [email protected]/locale/customize-ibus.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Customize Ibus 14\n"
"Project-Id-Version: Customize Ibus 15\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-04-19 19:15+0800\n"
"POT-Creation-Date: 2021-04-20 12:59+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,26 +17,18 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: extension.js:213
#: extension.js:211
msgid "loading background for IBus:"
msgstr ""

#: extension.js:314
#: extension.js:311
msgid "loading user theme for IBus:"
msgstr ""

#: extension.js:320
msgid "Copied from Source File: "
msgstr ""

#: extension.js:326
#: extension.js:315
msgid "loading default theme for IBus"
msgstr ""

#: extension.js:380
msgid "Restarting..."
msgstr ""

#: prefs.js:61
msgid "Custom IME theme"
msgstr ""
Expand Down
16 changes: 4 additions & 12 deletions [email protected]/locale/zh_CN/LC_MESSAGES/customize-ibus.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: customize-ibus 1\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-04-19 19:15+0800\n"
"POT-Creation-Date: 2021-04-20 12:52+0800\n"
"PO-Revision-Date: 2021-03-27 16:22+0800\n"
"Last-Translator: Hollow Man <[email protected]>\n"
"Language-Team: Chinese (simplified) <[email protected]>\n"
Expand All @@ -17,26 +17,18 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: extension.js:213
#: extension.js:211
msgid "loading background for IBus:"
msgstr "正在为IBus加载背景:"

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

#: extension.js:320
msgid "Copied from Source File: "
msgstr "拷贝自源文件:"

#: extension.js:326
#: extension.js:315
msgid "loading default theme for IBus"
msgstr "正在为IBus加载当前系统主题"

#: extension.js:380
msgid "Restarting..."
msgstr "正在重新启动⋯⋯"

#: prefs.js:61
msgid "Custom IME theme"
msgstr "自定义主题"
Expand Down
2 changes: 1 addition & 1 deletion [email protected]/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"original-authors": ["[email protected]"],
"uuid": "[email protected]",
"extension-id": "customize-ibus",
"version": 14
"version": 15
}
20 changes: 10 additions & 10 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const CustomizeIBus = GObject.registerClass(
});
this._fileChooser.connect("response", (dlg, response) => {
if (response !== Gtk.ResponseType.ACCEPT) return;
gsettings.set_string("custom-bg", dlg.get_file().get_path());
gsettings.set_string(Fields.CUSTOMBG, dlg.get_file().get_path());
});

this._logoPicker = new Gtk.Button({
Expand All @@ -96,7 +96,7 @@ const CustomizeIBus = GObject.registerClass(
this._fileChooser.show();
});
gsettings.connect(
"changed::custom-bg",
`changed::${Fields.CUSTOMBG}`,
this._updateLogoPicker.bind(this)
);
this._updateLogoPicker();
Expand All @@ -110,7 +110,7 @@ const CustomizeIBus = GObject.registerClass(
});
this._cssFileChooser.connect("response", (dlg, response) => {
if (response !== Gtk.ResponseType.ACCEPT) return;
gsettings.set_string("custom-theme", dlg.get_file().get_path());
gsettings.set_string(Fields.CUSTOMTHEME, dlg.get_file().get_path());
});

this._cssPicker = new Gtk.Button({
Expand All @@ -121,20 +121,20 @@ const CustomizeIBus = GObject.registerClass(
this._cssFileChooser.show();
});
gsettings.connect(
"changed::custom-theme",
`changed::${Fields.CUSTOMTHEME}`,
this._updateCssPicker.bind(this)
);
this._updateCssPicker();
}

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

_updateCssPicker() {
const filename = gsettings.get_string("custom-theme");
const filename = gsettings.get_string(Fields.CUSTOMTHEME);
if (!GLib.basename(filename)) this._cssPicker.label = _("(None)");
else this._cssPicker.label = GLib.basename(filename);
}
Expand Down Expand Up @@ -163,18 +163,18 @@ const CustomizeIBus = GObject.registerClass(
});
this._field_use_custom_font.connect("notify::active", (widget) => {
this._field_custom_font.set_sensitive(widget.active);
ibusGsettings.set_boolean("use-custom-font", widget.active);
ibusGsettings.set_boolean(Fields.USECUSTOMFONT, widget.active);
});
this._field_use_custom_bg.connect("notify::active", (widget) => {
this._logoPicker.set_sensitive(widget.active);
ibusGsettings.set_boolean("use-custom-bg", widget.active);
ibusGsettings.set_boolean(Fields.USECUSTOMBG, widget.active);
});
this._field_enable_custom_theme.connect("notify::active", (widget) => {
this._cssPicker.set_sensitive(widget.active);
ibusGsettings.set_boolean("enable-custom-theme", widget.active);
ibusGsettings.set_boolean(Fields.ENABLECUSTOMTHEME, widget.active);
});
this._field_custom_font.connect("font-set", (widget) => {
ibusGsettings.set_string("custom-font", widget.font_name);
ibusGsettings.set_string(Fields.CUSTOMFONT, widget.font_name);
gsettings.set_string(Fields.CUSTOMFONT, widget.font_name);
});

Expand Down
3 changes: 0 additions & 3 deletions [email protected]/stylesheet.css

This file was deleted.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "customize-ibus",
"version": "14.0.0",
"version": "15.0.0",
"description": "Customize IBus for orientation, shell theme, background picture, font and ascii mode auto-switch.",
"dependencies": {
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"husky": "^5.1.3",
"eslint": "^7.24.0",
"eslint-config-prettier": "^8.2.0",
"eslint-plugin-prettier": "^3.4.0",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1"
},
Expand Down

0 comments on commit 8aa6cbe

Please sign in to comment.