Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add Media widget #13

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified requirements.txt
Binary file not shown.
21 changes: 20 additions & 1 deletion src/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bars:
right: 0
widgets:
left: ["komorebi_workspaces", "komorebi_active_layout", "active_window"]
center: ["clock"]
center: ["windows_media"]
right: ["explorer_button", "ip_info", "cpu", "memory", "battery"]

# widgets:
Expand Down Expand Up @@ -482,3 +482,22 @@ widgets:
# run every hour
run_interval: 3600000
return_format: "json"

windows_media:
type: "win32.media_player.MediaWidget"
options:
label: "{media[title]} - {media[artist]}"
label_alt: "{media[title]} - {media[artist]}"
update_interval: 1000
keep_thumbnail_aspect_ratio: false
layout: ["thumbnail", "label"]
icons:
shuffle: "\uf074"
play: "\uf04b"
pause: "\uf04c"
next: "\uf051"
prev: "\uf048"
close: "\uf00d"
repeat_off: "\uf2f9"
repeat_track: "\uf2f9 (T)"
repeat_list: "\uf2f9 (L)"
15 changes: 14 additions & 1 deletion src/core/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from core.validation.bar import BAR_DEFAULTS
from BlurWindow.blurWindow import GlobalBlur


try:
from core.utils.win32 import app_bar
IMPORT_APP_BAR_MANAGER_SUCCESSFUL = True
Expand Down Expand Up @@ -81,6 +82,18 @@ def __init__(
def bar_id(self) -> str:
return self._bar_id

@property
def dimensions(self):
return self._dimensions

@property
def alignment(self):
return self._alignment

@property
def name(self):
return self._bar_name

def on_geometry_changed(self, geo: QRect) -> None:
logging.info(f"Screen geometry changed. Updating position for bar ({self.bar_id})")
self.position_bar()
Expand Down Expand Up @@ -159,7 +172,7 @@ def _add_widgets(self, widgets: dict[str, list] = None):
for widget in widgets[layout_type]:
widget.setFixedHeight(self._bar_frame.geometry().height())
widget.parent_layout_type = layout_type
widget.bar_id = self.bar_id
widget.bar = self
layout.addWidget(widget, 0)

if layout_type in ["left", "center"]:
Expand Down
2 changes: 1 addition & 1 deletion src/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def init_logger():
logging.basicConfig(
level=logging.DEBUG,
level=logging.INFO,
filename=join(get_config_dir(), DEFAULT_LOG_FILENAME),
format=LOG_FORMAT,
datefmt=LOG_DATETIME,
Expand Down
83 changes: 74 additions & 9 deletions src/core/utils/win32/media_control.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from enum import Enum
from PyQt6.QtGui import QImage
from winrt.windows.media.control import GlobalSystemMediaTransportControlsSessionManager
from winrt.windows.storage.streams import DataReader, Buffer, InputStreamOptions

THUMBNAIL_BUFFER_SIZE = 5 * 1024 * 1024


class WindowsMediaRepeat(Enum):
Off = 0
Track = 1
List = 2


async def get_current_session():
"""
Expand All @@ -16,22 +26,77 @@ async def get_current_session():
return sessions.get_current_session()


def props_to_dict(props):
return {
attr: props.__getattribute__(attr) for attr in dir(props) if attr[0] != '_'
}


async def get_media_info():
"""
{
'album_artist': '',
'album_title': '',
'album_track_count': 0,
'artist': 'Some Artist',
'playback_type': 1,
'subtitle': '',
'thumbnail': <_winrt_Windows_Storage_Streams.IRandomAccessStreamReference>,
'title': 'Some Title',
'track_number': 0
}
"""
current_session = await get_current_session()

if current_session:
media_props = await current_session.try_get_media_properties_async()
return {
song_attr: media_props.__getattribute__(song_attr)
for song_attr in dir(media_props)
if song_attr[0] != '_'
}
media_props = props_to_dict(media_props)
del media_props['genres']
return media_props


async def get_playback_info():
"""
{
'auto_repeat_mode': None,
'controls': {
'is_channel_down_enabled': False,
'is_channel_up_enabled': False,
'is_fast_forward_enabled': False,
'is_next_enabled': False,
'is_pause_enabled': False,
'is_play_enabled': True,
'is_play_pause_toggle_enabled': True,
'is_playback_position_enabled': False,
'is_playback_rate_enabled': False,
'is_previous_enabled': False,
'is_record_enabled': False,
'is_repeat_enabled': False,
'is_rewind_enabled': False,
'is_shuffle_enabled': False,
'is_stop_enabled': True
},
'is_shuffle_active': None,
'playback_rate': None,
'playback_status': 5,
'playback_type': 1
}
"""
current_session = await get_current_session()

if current_session:
playback_props = current_session.get_playback_info()
playback_props = props_to_dict(playback_props)
playback_props['controls'] = props_to_dict(playback_props['controls'])
return playback_props


async def read_stream_into_buffer(thumbnail_ref) -> bytearray:
buffer = Buffer(5000000)
async def stream_to_image(thumbnail_ref) -> QImage:
buffer = Buffer(THUMBNAIL_BUFFER_SIZE)
readable_stream = await thumbnail_ref.open_read_async()
readable_stream.read_async(buffer, buffer.capacity, InputStreamOptions.READ_AHEAD)
await readable_stream.read_async(buffer, buffer.capacity, InputStreamOptions.READ_AHEAD)
buffer_reader = DataReader.from_buffer(buffer)
thumbnail_buffer = buffer_reader.read_bytes(buffer.length)
return bytearray(thumbnail_buffer)
thumbnail_image = QImage()
thumbnail_image.loadFromData(bytearray(thumbnail_buffer))
return thumbnail_image
88 changes: 88 additions & 0 deletions src/core/validation/widgets/win32/media_player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
DEFAULTS = {
'label': "{media[title]} - {media[artist]}",
'label_alt': "{media[title]} - {media[artist]}",
'update_interval': 1000,
'keep_thumbnail_aspect_ratio': False,
'layout': ["thumbnail", "label", "close"],
'icons': {
'shuffle': "\uf074",
'play': "\uf04b",
'pause': "\uf04c",
'repeat_off': "\uf2f9",
'repeat_track': "\uf2f9 (T)",
'repeat_list': "\uf2f9 (L)",
'next': "\uf051",
'prev': "\uf048",
'close': "\uf00d"
}
}

VALIDATION_SCHEMA = {
'label': {
'type': 'string',
'default': DEFAULTS['label']
},
'label_alt': {
'type': 'string',
'default': DEFAULTS['label_alt']
},
'update_interval': {
'type': 'integer',
'default': DEFAULTS['update_interval'],
'min': 0
},
'keep_thumbnail_aspect_ratio': {
'type': 'boolean',
'default': DEFAULTS['keep_thumbnail_aspect_ratio']
},
'layout': {
'type': 'list',
'schema': {
'type': 'string',
'allowed': ["thumbnail", "label", "close", "prev", "play_pause", "next", "shuffle", "repeat"]
},
'default': DEFAULTS['layout']
},
'icons': {
'type': 'dict',
'schema': {
'shuffle': {
'type': 'string',
'default': DEFAULTS['icons']['shuffle']
},
'play': {
'type': 'string',
'default': DEFAULTS['icons']['play']
},
'pause': {
'type': 'string',
'default': DEFAULTS['icons']['pause']
},
'repeat_off': {
'type': 'string',
'default': DEFAULTS['icons']['repeat_off']
},
'repeat_track': {
'type': 'string',
'default': DEFAULTS['icons']['repeat_track']
},
'repeat_list': {
'type': 'string',
'default': DEFAULTS['icons']['repeat_list']
},
'next': {
'type': 'string',
'default': DEFAULTS['icons']['next']
},
'prev': {
'type': 'string',
'default': DEFAULTS['icons']['prev']
},
'close': {
'type': 'string',
'default': DEFAULTS['icons']['close']
}
},
'default': DEFAULTS['icons']
}
}
2 changes: 1 addition & 1 deletion src/core/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(
self._widget_frame_layout = QHBoxLayout()
self.widget_layout = QHBoxLayout()
self.timer_interval = timer_interval
self.bar_id = None
self.bar = None

if class_name:
self._widget_frame.setProperty("class", f"widget {class_name}")
Expand Down
Loading