Skip to content

Commit

Permalink
[Feature] Trayless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
melianmiko committed Oct 14, 2024
1 parent a539e28 commit 3e4f2f8
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 71 deletions.
4 changes: 2 additions & 2 deletions openfreebuds/driver/generic/spp.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ async def stop(self):
await self.__task_recv
self.__task_recv = None

self._writer.close()
# await self._writer.wait_closed()
if self._writer is not None:
self._writer.close()

self._writer = None
self.started = False
Expand Down
16 changes: 11 additions & 5 deletions openfreebuds_qt/app/dialog/first_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import webbrowser

from PyQt6.QtCore import pyqtSlot
from PyQt6.QtWidgets import QDialog
from PyQt6.QtWidgets import QDialog, QSystemTrayIcon
from qasync import asyncSlot

import openfreebuds_backend
from openfreebuds import OfbEventKind
from openfreebuds_qt.config import OfbQtConfigParser
from openfreebuds_qt.constants import LINK_WEBSITE_HELP
from openfreebuds_qt.designer.first_run_dialog import Ui_OfbQtFirstRunDialog
Expand All @@ -26,7 +27,8 @@ def __init__(self, ctx):

self.autostart_checkbox.setChecked(not self.config.is_containerized_app)
self.autostart_checkbox.setEnabled(not self.config.is_containerized_app)
self.linux_notice.setVisible(sys.platform == 'linux')
self.background_checkbox.setChecked(QSystemTrayIcon.isSystemTrayAvailable())
self.background_checkbox.setEnabled(QSystemTrayIcon.isSystemTrayAvailable())

preview_fn = "ofb_linux_preview" if sys.platform == 'linux' else "ofb_win32_preview"
preview_image = get_img_colored(preview_fn,
Expand All @@ -39,12 +41,16 @@ async def on_confirm(self):
async with qt_error_handler("OfbQtFirstRunDialog_Confirm", self.ctx):
self.hide()

if self.autostart_checkbox.isChecked():
openfreebuds_backend.set_run_at_boot(True)

openfreebuds_backend.set_run_at_boot(self.autostart_checkbox.isChecked())
self.config.set("ui", "background", self.background_checkbox.isChecked())
self.config.set("ui", "first_run_finished", True)
self.config.save()

if not self.background_checkbox.isChecked():
self.ctx.main_window.show()

await self.ctx.ofb.send_message(OfbEventKind.QT_SETTINGS_CHANGED)

@pyqtSlot()
def on_faq_click(self):
webbrowser.open(LINK_WEBSITE_HELP)
14 changes: 10 additions & 4 deletions openfreebuds_qt/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from PyQt6.QtCore import pyqtSlot
from PyQt6.QtGui import QIcon, QKeySequence
from PyQt6.QtWidgets import QMenu
from qasync import asyncSlot
from PyQt6.QtWidgets import QMenu, QSystemTrayIcon
from qasync import asyncSlot, asyncClose

from openfreebuds import IOpenFreebuds, OfbEventKind
from openfreebuds.utils.logger import create_logger
Expand Down Expand Up @@ -34,6 +34,7 @@ def __init__(self, ctx: IOfbQtApplication):
self.ctx = ctx
self.ofb = ctx.ofb
self.config = OfbQtConfigParser.get_instance()
self.tray_available = QSystemTrayIcon.isSystemTrayAvailable()

self.setupUi(self)

Expand Down Expand Up @@ -123,7 +124,7 @@ def _fill_extras_menu(self):
hide_action = self.extra_menu.addAction(self.tr("Close this window"))
hide_action.setShortcut(QKeySequence('Ctrl+W'))
# noinspection PyUnresolvedReferences
hide_action.triggered.connect(self.hide)
hide_action.triggered.connect(self.hide_or_exit)

exit_action = self.extra_menu.addAction(self.tr("Exit OpenFreebuds"))
exit_action.setShortcut(QKeySequence('Ctrl+Q'))
Expand Down Expand Up @@ -212,9 +213,14 @@ def _device_section_set_visible(self, visible):
def closeEvent(self, e):
if self.isVisible():
e.ignore()
self.hide()
self.hide_or_exit()
return

def hide_or_exit(self):
self.hide()
if not self.config.get("ui", "background", True) or not self.tray_available:
self.on_exit()

def showEvent(self, e):
e.accept()
self._ui_update_task = asyncio.create_task(self._update_loop())
Expand Down
16 changes: 15 additions & 1 deletion openfreebuds_qt/app/module/ui_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys

from PyQt6.QtCore import QLocale
from PyQt6.QtWidgets import QSystemTrayIcon
from qasync import asyncSlot

from openfreebuds import OfbEventKind
Expand All @@ -11,7 +12,7 @@
from openfreebuds_qt.config import OfbQtConfigParser
from openfreebuds_qt.designer.ui_settings import Ui_OfbQtUiSettingsModule
from openfreebuds_qt.qt_i18n import get_shortcut_names
from openfreebuds_qt.utils import blocked_signals, list_available_locales
from openfreebuds_qt.utils import blocked_signals, list_available_locales, OfbCoreEvent

log = create_logger("OfbQtUiSettingsModule")

Expand Down Expand Up @@ -67,12 +68,25 @@ def __init__(self, *args, **kwargs):
with blocked_signals(self.tray_dc_toggle):
self.tray_dc_toggle.setChecked(self.config.get("ui", "tray_show_dual_connect", False))

async def update_ui(self, event: OfbCoreEvent):
if not event.kind_match(OfbEventKind.QT_SETTINGS_CHANGED):
return

with blocked_signals(self.autostart_toggle):
self.autostart_toggle.setChecked(is_run_at_boot())

if self.config.is_containerized_app:
self.autostart_toggle.setVisible(False)

with blocked_signals(self.background_toggle):
self.background_toggle.setEnabled(QSystemTrayIcon.isSystemTrayAvailable())
self.background_toggle.setChecked(self.config.get("ui", "background", True))

@asyncSlot(bool)
async def on_background_toggle(self, value: bool):
self.config.set("ui", "background", value)
self.config.save()

@asyncSlot(bool)
async def on_autostart_toggle(self, value: bool):
set_run_at_boot(value)
Expand Down
30 changes: 12 additions & 18 deletions openfreebuds_qt/designer/first_run_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,10 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="linux_notice">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>If you're running under GNOME shell and can't find tray icon, please, check FAQ.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand All @@ -170,6 +154,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="background_checkbox">
<property name="text">
<string>Mininize to system tray instead of closing</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
Expand Down Expand Up @@ -204,7 +198,7 @@
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand Down
29 changes: 16 additions & 13 deletions openfreebuds_qt/designer/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<height>16777215</height>
</size>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarPolicy::ScrollBarAlwaysOff</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
Expand All @@ -76,8 +79,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>208</width>
<height>598</height>
<width>210</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -164,7 +167,7 @@
<string notr="true">OpenFreebuds</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
<set>Qt::AlignmentFlag::AlignLeading|Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignVCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -334,7 +337,7 @@
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -373,7 +376,7 @@
<string notr="true">10%</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -458,7 +461,7 @@
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -497,7 +500,7 @@
<string notr="true">10%</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -582,7 +585,7 @@
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -621,7 +624,7 @@
<string notr="true">10%</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
<set>Qt::AlignmentFlag::AlignCenter</set>
</property>
</widget>
</item>
Expand Down Expand Up @@ -764,7 +767,7 @@
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand Down Expand Up @@ -854,8 +857,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>738</width>
<height>598</height>
<width>740</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -865,7 +868,7 @@
</sizepolicy>
</property>
<property name="autoFillBackground">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</widget>
Expand Down
Loading

0 comments on commit 3e4f2f8

Please sign in to comment.