| 1 |
# COPYRIGHT (C) 2022-2023 Nicotine+ Contributors |
| 2 |
# |
| 3 |
# GNU GENERAL PUBLIC LICENSE |
| 4 |
# Version 3, 29 June 2007 |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from gi.repository import Gtk |
| 20 |
|
| 21 |
from pynicotine.core import core |
| 22 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 23 |
from pynicotine.gtkgui.widgets.popover import Popover |
| 24 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 25 |
|
| 26 |
|
| 27 |
class ChatCommandHelp(Popover): |
| 28 |
|
| 29 |
def __init__(self, window, interface): |
| 30 |
|
| 31 |
self.interface = interface |
| 32 |
self.scrollable = Gtk.ScrolledWindow( |
| 33 |
propagate_natural_height=True, propagate_natural_width=True, visible=True |
| 34 |
) |
| 35 |
self.container = None |
| 36 |
|
| 37 |
super().__init__( |
| 38 |
window=window, |
| 39 |
content_box=self.scrollable, |
| 40 |
show_callback=self._on_show, |
| 41 |
width=600, |
| 42 |
height=450 |
| 43 |
) |
| 44 |
|
| 45 |
def _create_command_section(self, group_name): |
| 46 |
|
| 47 |
section_container = Gtk.Box( |
| 48 |
orientation=Gtk.Orientation.VERTICAL, margin_start=18, margin_end=18, margin_top=18, margin_bottom=18, |
| 49 |
spacing=12, visible=True |
| 50 |
) |
| 51 |
section_label = Gtk.Label(label=group_name, selectable=True, wrap=True, xalign=0, visible=True) |
| 52 |
|
| 53 |
add_css_class(section_label, "heading") |
| 54 |
|
| 55 |
if GTK_API_VERSION >= 4: |
| 56 |
section_container.append(section_label) # pylint: disable=no-member |
| 57 |
self.container.append(section_container) # pylint: disable=no-member |
| 58 |
else: |
| 59 |
section_container.add(section_label) # pylint: disable=no-member |
| 60 |
self.container.add(section_container) # pylint: disable=no-member |
| 61 |
|
| 62 |
return section_container |
| 63 |
|
| 64 |
def _create_command_row(self, parent, command, aliases, parameters, description): |
| 65 |
|
| 66 |
row = Gtk.Box(homogeneous=True, spacing=12, visible=True) |
| 67 |
command_label = Gtk.Label( |
| 68 |
label=f"/{', /'.join([command] + aliases)} {' '.join(parameters)}".strip(), |
| 69 |
selectable=True, wrap=True, xalign=0, yalign=0, visible=True |
| 70 |
) |
| 71 |
description_label = Gtk.Label( |
| 72 |
label=description, selectable=True, wrap=True, xalign=0, yalign=0, |
| 73 |
visible=True |
| 74 |
) |
| 75 |
|
| 76 |
add_css_class(command_label, "italic") |
| 77 |
|
| 78 |
if GTK_API_VERSION >= 4: |
| 79 |
row.append(command_label) # pylint: disable=no-member |
| 80 |
row.append(description_label) # pylint: disable=no-member |
| 81 |
|
| 82 |
parent.append(row) # pylint: disable=no-member |
| 83 |
else: |
| 84 |
row.add(command_label) # pylint: disable=no-member |
| 85 |
row.add(description_label) # pylint: disable=no-member |
| 86 |
|
| 87 |
parent.add(row) # pylint: disable=no-member |
| 88 |
|
| 89 |
return row |
| 90 |
|
| 91 |
def _on_show(self, *_args): |
| 92 |
|
| 93 |
if self.container is not None: |
| 94 |
if GTK_API_VERSION >= 4: |
| 95 |
self.scrollable.set_child(None) # pylint: disable=no-member |
| 96 |
else: |
| 97 |
self.scrollable.remove(self.container) # pylint: disable=no-member |
| 98 |
|
| 99 |
self.container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, visible=True) |
| 100 |
|
| 101 |
for group_name, command_data in core.pluginhandler.get_command_groups_data(self.interface).items(): |
| 102 |
section_container = self._create_command_section(group_name) |
| 103 |
|
| 104 |
for command, aliases, parameters, description in command_data: |
| 105 |
self._create_command_row(section_container, command, aliases, parameters, description) |
| 106 |
|
| 107 |
if GTK_API_VERSION >= 4: |
| 108 |
self.scrollable.set_child(self.container) # pylint: disable=no-member |
| 109 |
else: |
| 110 |
self.scrollable.add(self.container) # pylint: disable=no-member |
| 111 |
|
| 112 |
self.container.child_focus(Gtk.DirectionType.TAB_FORWARD) |