Switch some calls to gtk.util helper methods

This commit is contained in:
Philipp Hörist 2018-10-28 14:59:51 +01:00
parent 6b5d559d7a
commit c109db6eeb
8 changed files with 65 additions and 115 deletions

View File

@ -56,6 +56,7 @@ from gajim.common.exceptions import GajimGeneralException
from gajim.gtk.dialogs import *
from gajim.gtk.add_contact import AddNewContactWindow
from gajim.gtk.util import get_icon_name
from gajim.gtk.util import resize_window
log = logging.getLogger('gajim.dialogs')
@ -1559,7 +1560,7 @@ class DataFormWindow(Dialog):
Gtk.ResponseType.CANCEL), (Gtk.STOCK_OK, Gtk.ResponseType.OK)],
on_response_ok=self.on_ok)
self.set_resizable(True)
gtkgui_helpers.resize_window(self, 600, 400)
resize_window(self, 600, 400)
self.dataform_widget = dataforms_widget.DataFormWidget()
self.dataform = dataforms.extend_form(node=form)
self.dataform_widget.set_sensitive(True)

View File

@ -23,7 +23,8 @@
import time
import datetime
from enum import IntEnum, unique
from enum import IntEnum
from enum import unique
from gi.repository import Gtk
from gi.repository import Gdk
@ -33,12 +34,19 @@ from gajim.common import app
from gajim.common import helpers
from gajim.common import exceptions
from gajim.common.i18n import _
from gajim.common.const import ShowConstant, KindConstant
from gajim.common.const import ShowConstant
from gajim.common.const import KindConstant
from gajim import conversation_textview
from gajim.gtk import util
from gajim.gtk.util import python_month, gtk_month
from gajim.gtk.util import python_month
from gajim.gtk.util import gtk_month
from gajim.gtk.util import resize_window
from gajim.gtk.util import move_window
from gajim.gtk.util import get_icon_name
from gajim.gtk.util import get_completion_liststore
from gajim.gtk.util import get_builder
from gajim.gtk.dialogs import ErrorDialog
@unique
@ -66,7 +74,7 @@ class HistoryWindow:
"""
def __init__(self, jid=None, account=None):
xml = util.get_builder('history_window.ui')
xml = get_builder('history_window.ui')
self.window = xml.get_object('history_window')
self.window.set_application(app.app)
self.calendar = xml.get_object('calendar')
@ -137,12 +145,12 @@ class HistoryWindow:
else:
self._load_history(None)
util.resize_window(self.window,
app.config.get('history_window_width'),
app.config.get('history_window_height'))
util.move_window(self.window,
app.config.get('history_window_x-position'),
app.config.get('history_window_y-position'))
resize_window(self.window,
app.config.get('history_window_width'),
app.config.get('history_window_height'))
move_window(self.window,
app.config.get('history_window_x-position'),
app.config.get('history_window_y-position'))
xml.connect_signals(self)
self.window.show_all()
@ -163,7 +171,7 @@ class HistoryWindow:
{key : (jid, account, nick_name, full_completion_name}
This is a generator and does pseudo-threading via idle_add().
"""
liststore = util.get_completion_liststore(
liststore = get_completion_liststore(
self.jid_entry.get_child())
liststore.set_sort_column_id(1, Gtk.SortType.ASCENDING)
self.jid_entry.get_child().get_completion().connect(
@ -183,8 +191,8 @@ class HistoryWindow:
completion_dict.update(
helpers.get_contact_dict_for_account(account))
muc_active_icon = util.get_icon_name('muc-active')
online_icon = util.get_icon_name('online')
muc_active_icon = get_icon_name('muc-active')
online_icon = get_icon_name('online')
keys = list(completion_dict.keys())
# Move the actual jid at first so we load history faster

View File

@ -24,12 +24,14 @@ import os
from gi.repository import Gtk
from gajim import dialogs
from gajim import gtkgui_helpers
from gajim.common import app
from gajim.common import helpers
from gajim.common.i18n import _
from gajim.gtk.util import get_builder
from gajim.gtk.util import get_icon_name
from gajim.gtk.util import move_window
from gajim.gtk.single_message import SingleMessageWindow
from gajim.gtk.tooltips import NotificationAreaTooltip
@ -382,9 +384,9 @@ class StatusIcon:
if not win.get_property('visible'):
win.show_all()
if app.config.get('save-roster-position'):
gtkgui_helpers.move_window(win,
app.config.get('roster_x-position'),
app.config.get('roster_y-position'))
move_window(win,
app.config.get('roster_x-position'),
app.config.get('roster_y-position'))
if not app.config.get('roster_window_skip_taskbar'):
win.set_property('skip-taskbar-hint', False)
win.present_with_time(Gtk.get_current_event_time())
@ -398,9 +400,9 @@ class StatusIcon:
win = app.interface.roster.window
if not win.get_property('visible') and app.config.get(
'save-roster-position'):
gtkgui_helpers.move_window(win,
app.config.get('roster_x-position'),
app.config.get('roster_y-position'))
move_window(win,
app.config.get('roster_x-position'),
app.config.get('roster_y-position'))
app.interface.handle_event(account, jid, event.type_)
def on_middle_click(self):

View File

@ -262,3 +262,12 @@ def convert_rgb_to_hex(rgb_string: str) -> str:
green = int(rgb.green * 255)
blue = int(rgb.blue * 255)
return '#%02x%02x%02x' % (red, green, blue)
def get_monitor_scale_factor() -> int:
display = Gdk.Display.get_default()
monitor = display.get_primary_monitor()
if monitor is None:
log.warning('Could not determine scale factor')
return 1
return monitor.get_scale_factor()

View File

@ -97,13 +97,6 @@ if os.name == 'nt':
from gajim.common import helpers
def get_total_screen_geometry():
screen = Gdk.Screen.get_default()
window = Gdk.Screen.get_root_window(screen)
w, h = window.get_width(), window.get_height()
log.debug('Get screen geometry: %s %s', w, h)
return w, h
def add_image_to_button(button, icon_name):
img = Gtk.Image()
path_img = get_icon_path(icon_name)
@ -153,55 +146,6 @@ def get_gtk_builder(file_name, widget=None):
builder.add_from_file(file_path)
return builder
def get_completion_liststore(entry):
"""
Create a completion model for entry widget completion list consists of
(Pixbuf, Text) rows
"""
completion = Gtk.EntryCompletion()
liststore = Gtk.ListStore(str, str)
render_pixbuf = Gtk.CellRendererPixbuf()
completion.pack_start(render_pixbuf, False)
completion.add_attribute(render_pixbuf, 'icon_name', 0)
render_text = Gtk.CellRendererText()
completion.pack_start(render_text, True)
completion.add_attribute(render_text, 'text', 1)
completion.set_property('text_column', 1)
completion.set_model(liststore)
entry.set_completion(completion)
return liststore
def move_window(window, x, y):
"""
Move the window, but also check if out of screen
"""
screen_w, screen_h = get_total_screen_geometry()
if x < 0:
x = 0
if y < 0:
y = 0
w, h = window.get_size()
if x + w > screen_w:
x = screen_w - w
if y + h > screen_h:
y = screen_h - h
window.move(x, y)
def resize_window(window, w, h):
"""
Resize window, but also checks if huge window or negative values
"""
screen_w, screen_h = get_total_screen_geometry()
if not w or not h:
return
if w > screen_w:
w = screen_w
if h > screen_h:
h = screen_h
window.resize(abs(w), abs(h))
def at_the_end(widget):
"""Determines if a Scrollbar in a GtkScrolledWindow is at the end.
@ -373,18 +317,6 @@ def get_fade_color(treeview, selected, focused):
return Gdk.RGBA(bg.red*p + fg.red*q, bg.green*p + fg.green*q,
bg.blue*p + fg.blue*q)
def make_gtk_month_python_month(month):
"""
GTK starts counting months from 0, so January is 0 but Python's time start
from 1, so align to Python
NOTE: Month MUST be an integer.
"""
return month + 1
def make_python_month_gtk_month(month):
return month - 1
def make_pixbuf_grayscale(pixbuf):
pixbuf2 = pixbuf.copy()
pixbuf.saturate_and_pixelate(pixbuf2, 0.0, False)
@ -805,11 +737,3 @@ def pango_to_css_weight(number):
if number > 900:
return 900
return int(math.ceil(number / 100.0)) * 100
def get_monitor_scale_factor():
display = Gdk.Display.get_default()
monitor = display.get_primary_monitor()
if monitor is None:
log.warning('Could not determine scale factor')
return 1
return monitor.get_scale_factor()

View File

@ -42,6 +42,8 @@ from gajim.chat_control import ChatControl
from gajim.gtk.dialogs import YesNoDialog
from gajim.gtk.util import get_icon_name
from gajim.gtk.util import resize_window
from gajim.gtk.util import move_window
####################
@ -185,7 +187,7 @@ class MessageWindow:
return sum(len(d) for d in self._controls.values())
def resize(self, width, height):
gtkgui_helpers.resize_window(self.window, width, height)
resize_window(self.window, width, height)
def _on_window_focus(self, widget, event):
# on destroy() the window that was last focused gets the focus
@ -1047,7 +1049,7 @@ class MessageWindowMgr(GObject.GObject):
else:
return
gtkgui_helpers.move_window(win.window, pos[0], pos[1])
move_window(win.window, pos[0], pos[1])
def _mode_to_key(self, contact, acct, type_, resource=None):
if self.mode == self.ONE_MSG_WINDOW_NEVER:
@ -1284,9 +1286,9 @@ class MessageWindowMgr(GObject.GObject):
child = w.parent_paned.get_child2()
w.parent_paned.remove(child)
self.parent_win.lookup_action('show-roster').set_enabled(False)
gtkgui_helpers.resize_window(w.window,
app.config.get('roster_width'),
app.config.get('roster_height'))
resize_window(w.window,
app.config.get('roster_width'),
app.config.get('roster_height'))
self._windows = {}

View File

@ -39,6 +39,8 @@ from gajim.common import ged
from gajim.common.i18n import _
from gajim.gtk.util import get_icon_name
from gajim.gtk.util import get_monitor_scale_factor
from gajim.gtk.util import get_total_screen_geometry
log = logging.getLogger('gajim.notify')
@ -172,7 +174,7 @@ class Notification:
app.interface.roster.popup_notification_windows.append(instance)
return
scale = gtkgui_helpers.get_monitor_scale_factor()
scale = get_monitor_scale_factor()
icon_pixbuf = gtkgui_helpers.gtk_icon_theme.load_icon_for_scale(
icon_name, 48, scale, 0)
@ -300,7 +302,7 @@ class PopupNotificationWindow:
window_width, self.window_height = self.window.get_size()
app.interface.roster.popups_notification_height += self.window_height
pos_x = app.config.get('notification_position_x')
screen_w, screen_h = gtkgui_helpers.get_total_screen_geometry()
screen_w, screen_h = get_total_screen_geometry()
if pos_x < 0:
pos_x = screen_w - window_width + pos_x + 1
pos_y = app.config.get('notification_position_y')
@ -337,7 +339,7 @@ class PopupNotificationWindow:
current_index += 1
window_width, window_height = window_instance.window.get_size()
app.interface.roster.popups_notification_height += window_height
screen_w, screen_h = gtkgui_helpers.get_total_screen_geometry()
screen_w, screen_h = get_total_screen_geometry()
window_instance.window.move(screen_w - window_width,
screen_h - \
app.interface.roster.popups_notification_height)

View File

@ -83,6 +83,8 @@ from gajim.gtk.history import HistoryWindow
from gajim.gtk.accounts import AccountsWindow
from gajim.gtk.tooltips import RosterTooltip
from gajim.gtk.util import get_icon_name
from gajim.gtk.util import resize_window
from gajim.gtk.util import move_window
log = logging.getLogger('gajim.roster')
@ -2373,9 +2375,9 @@ class RosterWindow:
def on_message_window_delete(self, win_mgr, msg_win):
if app.config.get('one_message_window') == 'always_with_roster':
self.show_roster_vbox(True)
gtkgui_helpers.resize_window(self.window,
app.config.get('roster_width'),
app.config.get('roster_height'))
resize_window(self.window,
app.config.get('roster_width'),
app.config.get('roster_height'))
def close_all_from_dict(self, dic):
"""
@ -5704,13 +5706,13 @@ class RosterWindow:
if len(app.connections) < 2:
# Do not merge accounts if only one exists
self.regroup = False
gtkgui_helpers.resize_window(self.window,
app.config.get('roster_width'),
app.config.get('roster_height'))
resize_window(self.window,
app.config.get('roster_width'),
app.config.get('roster_height'))
if app.config.get('save-roster-position'):
gtkgui_helpers.move_window(self.window,
app.config.get('roster_x-position'),
app.config.get('roster_y-position'))
move_window(self.window,
app.config.get('roster_x-position'),
app.config.get('roster_y-position'))
self.popups_notification_height = 0
self.popup_notification_windows = []