From bf5390b0fdf8d2d3b9ec7d84bc0f133c2918dfe0 Mon Sep 17 00:00:00 2001 From: Leo Date: Fri, 7 Jul 2023 13:13:41 +0900 Subject: [PATCH] UserCard: Small Gtk4 prep (#675) --- src/Cards/UserCard.vala | 198 +++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 105 deletions(-) diff --git a/src/Cards/UserCard.vala b/src/Cards/UserCard.vala index 53c70976f..286c560b9 100644 --- a/src/Cards/UserCard.vala +++ b/src/Cards/UserCard.vala @@ -1,20 +1,6 @@ /* - * Copyright 2018–2021 elementary, Inc. (https://elementary.io) - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. + * Copyright 2018-2023 elementary, Inc. (https://elementary.io) + * SPDX-License-Identifier: GPL-3.0-or-later * * Authors: Corentin Noël */ @@ -38,24 +24,31 @@ public class Greeter.UserCard : Greeter.BaseCard { private Act.User act_user; private Pantheon.AccountsService greeter_act; private Pantheon.SettingsDaemon.AccountsService settings_act; + private Gtk.Revealer form_revealer; private Gtk.Stack login_stack; private Greeter.PasswordEntry password_entry; - private SelectionCheck logged_in; + private unowned Gtk.StyleContext logged_in_context; - private weak Gtk.StyleContext main_grid_style_context; - private weak Gtk.StyleContext password_entry_context; + private unowned Gtk.StyleContext main_box_style_context; + private unowned Gtk.StyleContext password_entry_context; private bool needs_settings_set = false; + public UserCard (LightDM.User lightdm_user) { + Object (lightdm_user: lightdm_user); + } + construct { need_password = true; var username_label = new Gtk.Label (lightdm_user.display_name) { hexpand = true, - margin = 24, - margin_bottom = 12 + margin_top = 24, + margin_bottom = 12, + margin_start = 24, + margin_end = 24, }; unowned var username_label_context = username_label.get_style_context (); @@ -69,30 +62,30 @@ public class Greeter.UserCard : Greeter.BaseCard { "connecting", password_entry, "sensitive", - GLib.BindingFlags.INVERT_BOOLEAN + INVERT_BOOLEAN ); var fingerprint_image = new Gtk.Image.from_icon_name ( "fingerprint-symbolic", - Gtk.IconSize.BUTTON + BUTTON ); bind_property ( "use-fingerprint", fingerprint_image, "no-show-all", - GLib.BindingFlags.INVERT_BOOLEAN | GLib.BindingFlags.SYNC_CREATE + INVERT_BOOLEAN | SYNC_CREATE ); bind_property ( "use-fingerprint", fingerprint_image, "visible", - GLib.BindingFlags.SYNC_CREATE + SYNC_CREATE ); var session_button = new Greeter.SessionButton () { - valign = Gtk.Align.START + valign = START }; var caps_lock_revealer = new Greeter.CapsLockRevealer (); @@ -105,7 +98,7 @@ public class Greeter.UserCard : Greeter.BaseCard { password_grid.attach (fingerprint_image, 1, 0); password_grid.attach (caps_lock_revealer, 0, 1, 2); - var size_group = new Gtk.SizeGroup (Gtk.SizeGroupMode.VERTICAL); + var size_group = new Gtk.SizeGroup (VERTICAL); size_group.add_widget (password_entry); size_group.add_widget (session_button); @@ -116,66 +109,61 @@ public class Greeter.UserCard : Greeter.BaseCard { "connecting", login_button, "sensitive", - GLib.BindingFlags.INVERT_BOOLEAN + INVERT_BOOLEAN ); - var disabled_icon = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", Gtk.IconSize.MENU); + var disabled_icon = new Gtk.Image.from_icon_name ("changes-prevent-symbolic", MENU); var disabled_message = new Gtk.Label (_("Account disabled")); - var disabled_grid = new Gtk.Grid () { - column_spacing = 6, + var disabled_box = new Gtk.Box (HORIZONTAL, 6) { halign = Gtk.Align.CENTER, margin_top = 3 }; - disabled_grid.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL); - disabled_grid.add (disabled_icon); - disabled_grid.add (disabled_message); + disabled_box.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL); + disabled_box.add (disabled_icon); + disabled_box.add (disabled_message); login_stack = new Gtk.Stack (); login_stack.add_named (password_grid, "password"); login_stack.add_named (login_button, "button"); - login_stack.add_named (disabled_grid, "disabled"); + login_stack.add_named (disabled_box, "disabled"); - var form_grid = new Gtk.Grid () { - column_spacing = 6, - margin = 24, - margin_bottom = 12, + var form_box = new Gtk.Box (HORIZONTAL, 6) { margin_top = 12, - row_spacing = 12 + margin_bottom = 12, + margin_start = 24, + margin_end = 24 }; - form_grid.add (login_stack); - form_grid.add (session_button); + form_box.add (login_stack); + form_box.add (session_button); form_revealer = new Gtk.Revealer () { margin_bottom = 12, reveal_child = true, - transition_type = Gtk.RevealerTransitionType.SLIDE_DOWN + transition_type = SLIDE_DOWN, + child = form_box }; - form_revealer.add (form_grid); bind_property ( "show-input", form_revealer, "reveal-child", - GLib.BindingFlags.SYNC_CREATE + SYNC_CREATE ); var background_path = lightdm_user.background; if (background_path == null) { - string path = GLib.Path.build_filename ("/", "var", "lib", "lightdm-data", lightdm_user.name, "wallpaper"); - if (GLib.FileUtils.test (path, FileTest.EXISTS)) { - var background_directory = GLib.File.new_for_path (path); + string path = Path.build_filename ("/", "var", "lib", "lightdm-data", lightdm_user.name, "wallpaper"); + if (FileUtils.test (path, EXISTS)) { + var background_directory = File.new_for_path (path); try { - var enumerator = background_directory.enumerate_children ( - GLib.FileAttribute.STANDARD_NAME, - GLib.FileQueryInfoFlags.NONE - ); + var enumerator = background_directory.enumerate_children (FileAttribute.STANDARD_NAME, NONE); - GLib.FileInfo file_info; + FileInfo file_info; while ((file_info = enumerator.next_file ()) != null) { - if (file_info.get_file_type () == GLib.FileType.REGULAR) { + if (file_info.get_file_type () == REGULAR) { background_path = Path.build_filename (path, file_info.get_name ()); break; } @@ -188,36 +176,38 @@ public class Greeter.UserCard : Greeter.BaseCard { var background_image = new Greeter.BackgroundImage (background_path); - var main_grid = new Gtk.Grid () { - margin_bottom = 48, - orientation = Gtk.Orientation.VERTICAL + var main_box = new Gtk.Box (VERTICAL, 0) { + margin_bottom = 48 }; - main_grid.add (background_image); - main_grid.add (username_label); - main_grid.add (form_revealer); + main_box.add (background_image); + main_box.add (username_label); + main_box.add (form_revealer); - main_grid_style_context = main_grid.get_style_context (); - main_grid_style_context.add_class (Granite.STYLE_CLASS_CARD); - main_grid_style_context.add_class (Granite.STYLE_CLASS_ROUNDED); - main_grid_style_context.add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); + main_box_style_context = main_box.get_style_context (); + main_box_style_context.add_class (Granite.STYLE_CLASS_CARD); + main_box_style_context.add_class (Granite.STYLE_CLASS_ROUNDED); + main_box_style_context.add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); update_collapsed_class (); var avatar = new Hdy.Avatar (64, lightdm_user.display_name, true) { - margin = 6 + margin_top = 6, + margin_bottom = 6, + margin_start = 6, + margin_end = 6, + loadable_icon = new FileIcon (File.new_for_path (lightdm_user.image)) }; - avatar.loadable_icon = new FileIcon (File.new_for_path (lightdm_user.image)); var avatar_overlay = new Gtk.Overlay () { - halign = Gtk.Align.CENTER, + halign = CENTER, + valign = START, margin_top = 100, - valign = Gtk.Align.START + child = avatar }; - avatar_overlay.add (avatar); logged_in = new SelectionCheck () { - halign = Gtk.Align.END, - valign = Gtk.Align.END + halign = END, + valign = END }; logged_in_context = logged_in.get_style_context (); @@ -230,25 +220,28 @@ public class Greeter.UserCard : Greeter.BaseCard { } var card_overlay = new Gtk.Overlay () { - margin = 12 + margin_top = 12, + margin_bottom = 12, + margin_start = 12, + margin_end = 12, + child = main_box }; - card_overlay.add (main_grid); card_overlay.add_overlay (avatar_overlay); - add (card_overlay); + child = card_overlay; act_user = Act.UserManager.get_default ().get_user (lightdm_user.name); - act_user.bind_property ("locked", username_label, "sensitive", GLib.BindingFlags.INVERT_BOOLEAN); - act_user.bind_property ("locked", session_button, "visible", GLib.BindingFlags.INVERT_BOOLEAN); + act_user.bind_property ("locked", username_label, "sensitive", INVERT_BOOLEAN); + act_user.bind_property ("locked", session_button, "visible", INVERT_BOOLEAN); act_user.notify["is-loaded"].connect (on_act_user_loaded); on_act_user_loaded (); card_overlay.focus.connect ((direction) => { - if (direction == Gtk.DirectionType.LEFT) { + if (direction == LEFT) { go_left (); return true; - } else if (direction == Gtk.DirectionType.RIGHT) { + } else if (direction == RIGHT) { go_right (); return true; } @@ -267,7 +260,7 @@ public class Greeter.UserCard : Greeter.BaseCard { // This makes all the animations synchonous form_revealer.size_allocate.connect ((alloc) => { - var total_height = form_grid.get_allocated_height () + form_grid.margin_top + form_grid.margin_bottom; + var total_height = form_box.get_allocated_height () + form_box.margin_top + form_box.margin_bottom; reveal_ratio = (double)alloc.height / (double)total_height; }); @@ -339,18 +332,18 @@ public class Greeter.UserCard : Greeter.BaseCard { unowned string? act_path = act_user.get_object_path (); if (act_path != null) { try { - greeter_act = GLib.Bus.get_proxy_sync ( - GLib.BusType.SYSTEM, + greeter_act = Bus.get_proxy_sync ( + SYSTEM, "org.freedesktop.Accounts", act_path, - GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES + GET_INVALIDATED_PROPERTIES ); - settings_act = GLib.Bus.get_proxy_sync ( - GLib.BusType.SYSTEM, + settings_act = Bus.get_proxy_sync ( + SYSTEM, "org.freedesktop.Accounts", act_path, - GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES + GET_INVALIDATED_PROPERTIES ); is_24h = greeter_act.time_format != "12h"; @@ -360,7 +353,7 @@ public class Greeter.UserCard : Greeter.BaseCard { sleep_inactive_battery_timeout = greeter_act.sleep_inactive_battery_timeout; sleep_inactive_battery_type = greeter_act.sleep_inactive_battery_type; - ((GLib.DBusProxy) greeter_act).g_properties_changed.connect ((changed_properties, invalidated_properties) => { + ((DBusProxy) greeter_act).g_properties_changed.connect ((changed_properties, invalidated_properties) => { string time_format; changed_properties.lookup ("TimeFormat", "s", out time_format); is_24h = time_format != "12h"; @@ -408,9 +401,9 @@ public class Greeter.UserCard : Greeter.BaseCard { private void update_collapsed_class () { if (show_input) { - main_grid_style_context.remove_class ("collapsed"); + main_box_style_context.remove_class ("collapsed"); } else { - main_grid_style_context.add_class ("collapsed"); + main_box_style_context.add_class ("collapsed"); } } @@ -432,14 +425,14 @@ public class Greeter.UserCard : Greeter.BaseCard { Variant[] elements = {}; foreach (var layout in settings_act.keyboard_layouts) { - GLib.Variant first = new GLib.Variant.string (layout.backend); - GLib.Variant second = new GLib.Variant.string (layout.name); - GLib.Variant result = new GLib.Variant.tuple ({first, second}); + Variant first = new Variant.string (layout.backend); + Variant second = new Variant.string (layout.name); + Variant result = new Variant.tuple ({first, second}); elements += result; } - GLib.Variant layouts_list = new GLib.Variant.array (new VariantType ("(ss)"), elements); + Variant layouts_list = new Variant.array (new VariantType ("(ss)"), elements); settings.set_value ("sources", layouts_list); settings.set_value ("current", settings_act.active_keyboard_layout); @@ -484,9 +477,9 @@ public class Greeter.UserCard : Greeter.BaseCard { var night_light_settings = new GLib.Settings ("org.gnome.settings-daemon.plugins.color"); night_light_settings.set_value ("night-light-enabled", settings_act.night_light_enabled); - var latitude = new GLib.Variant.double (settings_act.night_light_last_coordinates.latitude); - var longitude = new GLib.Variant.double (settings_act.night_light_last_coordinates.longitude); - var coordinates = new GLib.Variant.tuple ({latitude, longitude}); + var latitude = new Variant.double (settings_act.night_light_last_coordinates.latitude); + var longitude = new Variant.double (settings_act.night_light_last_coordinates.longitude); + var coordinates = new Variant.tuple ({latitude, longitude}); night_light_settings.set_value ("night-light-last-coordinates", coordinates); night_light_settings.set_value ("night-light-schedule-automatic", settings_act.night_light_schedule_automatic); @@ -495,24 +488,19 @@ public class Greeter.UserCard : Greeter.BaseCard { night_light_settings.set_value ("night-light-temperature", settings_act.night_light_temperature); } - public UserCard (LightDM.User lightdm_user) { - Object (lightdm_user: lightdm_user); - } - public override void wrong_credentials () { - - weak Gtk.StyleContext entry_style_context = password_entry.get_style_context (); + unowned var entry_style_context = password_entry.get_style_context (); entry_style_context.add_class (Gtk.STYLE_CLASS_ERROR); - main_grid_style_context.add_class ("shake"); + main_box_style_context.add_class ("shake"); - GLib.Timeout.add (ERROR_SHAKE_DURATION, () => { - main_grid_style_context.remove_class ("shake"); + Timeout.add (ERROR_SHAKE_DURATION, () => { + main_box_style_context.remove_class ("shake"); entry_style_context.remove_class (Gtk.STYLE_CLASS_ERROR); connecting = false; password_entry.grab_focus (); - return GLib.Source.REMOVE; + return Source.REMOVE; }); }