如果您在EMS或設置%字體大小時,將立足於默認的字體大小。訣竅是根據窗口大小更改默認字體大小。連接到窗口的configure_event
,並調整窗口大小時設置新的默認字體大小。
這裏有一個簡單的例子:
const string WINDOW_CSS_TEMPLATE = "#mywindow { font-size: %fpx; }";
const string APPLICATION_CSS = "#mywindow .label { font-size: 5em; }";
void main(string [] args) {
Gtk.init(ref args);
var win = new Gtk.Window();
win.name = "mywindow";
win.default_width = 800;
win.default_height = 600;
var label = new Gtk.Label("Some text");
label.halign = Gtk.Align.END;
label.valign = Gtk.Align.END;
win.add(label);
win.destroy.connect(Gtk.main_quit);
var win_provider = new Gtk.CssProvider();
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
win_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
var app_provider = new Gtk.CssProvider();
try {
app_provider.load_from_data(APPLICATION_CSS, -1);
} catch (Error e) {
Process.exit(1);
}
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
app_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
win.configure_event.connect((event) => {
try {
// Replace this with your desired calculation of the base font size
var target_size = (double) event.height/600.0 * 12.0;
var css = WINDOW_CSS_TEMPLATE.printf(target_size);
win_provider.load_from_data(css, -1);
} catch (Error e) {
Process.exit(1);
}
return Gdk.EVENT_PROPAGATE;
});
win.show_all();
Gtk.main();
}
此只關心窗口的高度,因此,如果您調整窗口會更寬比它高,你會得到一個unreadably微小的標籤,但你可以插入任何計算工作給你。
完美地工作。就我而言,我們保證永遠不會改變窗口大小,所以我[在沒有事件的情況下實現您的解決方案](https://github.com/andybarry/pdfpc/commit/19e279cf0a9f6f6bcc89c79185f7bf1bdf305c2b) – abarry