Skip to content

Commit c63d8dd

Browse files
authored
Merge pull request #213 from trimclain/wifi-disabled
feat(wifi): add ethernet_label and ethernet_label_alt
2 parents f941863 + e1a6dc5 commit c63d8dd

4 files changed

Lines changed: 72 additions & 27 deletions

File tree

docs/widgets/(Widget)-Custom.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
| `label_alt` | string | `"{data}"` | Example of label alt. |
77
| `label_max_length` | int | `None` | The maximum length of the label. |
88
| `class_name` | string | `"custom-widget"` | The CSS class name for the widget. |
9-
| `exec_options` | dict | `{'run_cmd': None, 'run_interval': 120000, 'return_format': 'json', 'hide_empty: False', use_shell: True, endoding: None}` | Execution options for custom widget. |
9+
| `exec_options` | dict | `{'run_cmd': None, 'run_interval': 120000, 'return_format': 'json', 'hide_empty: False', use_shell: True, encoding: None}` | Execution options for custom widget. |
1010
| `callbacks` | dict | `{'on_left': 'toggle_label', 'on_middle': 'do_nothing', 'on_right': 'do_nothing'}` | Callbacks for mouse events. |
1111
| `animation` | dict | `{'enabled': True, 'type': 'fadeInOut', 'duration': 200}` | Animation settings for the widget. |
1212
| `container_padding` | dict | `{'top': 0, 'left': 0, 'bottom': 0, 'right': 0}` | Explicitly set padding inside widget container. |

docs/widgets/(Widget)-WiFi.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
| Option | Type | Default | Description |
44
|---------------------|---------|-------------------------------------------------------------------------|-----------------------------------------------------------------------------|
55
| `label` | string | `"{wifi_icon}"` | The label format for the WiFi widget. |
6-
| `label_alt` | string | `"{wifi_name} {wifi_strength}%"` | The alternative label format for the WiFi widget. |
6+
| `label_alt` | string | `"{wifi_icon} {wifi_name}"` | The alternative label format for the WiFi widget. |
77
| `update_interval` | integer | `1000` | Update interval in milliseconds. |
88
| `wifi_icons` | list | `[ "\udb82\udd2e", "\udb82\udd1f", "\udb82\udd22", "\udb82\udd25", "\udb82\udd28" ]` | Icons for different WiFi signal strengths. |
9+
| `ethernet_label` | string | `"{wifi_icon}"` | The label format during active Ethernet connection. |
10+
| `ethernet_label_alt` | string | `"{wifi_icon} {ip_addr}"` | The alternative label format during active Ethernet connection. |
911
| `ethernet_icon` | string | "\ueba9" | The icon to indicate Ethernet connection. |
1012
| `callbacks` | dict | `{ 'on_left': 'next_layout', 'on_middle': 'toggle_monocle', 'on_right': 'prev_layout' }` | Callbacks for mouse events on the widget. |
1113
| `animation` | dict | `{'enabled': True, 'type': 'fadeInOut', 'duration': 200}` | Animation settings for the widget. |
@@ -28,6 +30,8 @@ wifi:
2830
on_left: "exec cmd.exe /c start ms-settings:network"
2931
on_middle: "do_nothing"
3032
on_right: "toggle_label"
33+
ethernet_label: "<span>{wifi_icon}</span>"
34+
ethernet_label_alt: "<span>{wifi_icon}</span>{ip_addr}"
3135
ethernet_icon: "\ueba9"
3236
wifi_icons: [
3337
"\udb82\udd2e", # Icon for 0% strength
@@ -44,9 +48,11 @@ wifi:
4448
```
4549
4650
## Description of Options
47-
- **label:** The format string for the active window title. You can use placeholders like `{win[title]}` to dynamically insert window information. Default is `"{icon}"`.
48-
- **label_alt:** The format string for the active window title when the widget is in the alternative state. Default is `"{wifi_name} {wifi_strength}%"`.
51+
- **label:** The format string for the WiFi Widget. Default is `"{wifi_icon}"`.
52+
- **label_alt:** The format string for the WiFi Widget when the it's in the alternative state. Default is `"{wifi_icon} {wifi_name}"`.
4953
- **update_interval:** The interval in milliseconds at which the widget updates. Default is `1000`.
54+
- **ethernet_label:** The format string for the WiFi Widget during active Ethernet connection. Default is `"{wifi_icon}"`.
55+
- **ethernet_label_alt:** The format string for the WiFi Widget during active Ethernet connection when the widget is in the alternative state. Default is `"{wifi_icon} {ip_addr}"`.
5056
- **ethernet_icon**: The icon that indicates an active Ethernet connection. It will be used as `{wifi_icon}` whenever there's no active WiFi connection. Default is "\ueba9".
5157
- **wifi_icons:** A list of icons to use for different WiFi signal strengths. Default is `["\udb82\udd2e","\udb82\udd1f","\udb82\udd22","\udb82\udd25","\udb82\udd28",]`.
5258
- **callbacks:** A dictionary of callbacks for mouse events on the widget. Default is `{'on_left': 'toggle_label', 'on_middle': 'do_nothing', 'on_right': 'do_nothing'}`.

src/core/validation/widgets/yasb/wifi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"\udb82\udd25", # Icon for 50-74% strength
1515
"\udb82\udd28" # Icon for 75-100% strength
1616
],
17+
'ethernet_label': "{wifi_icon}",
18+
'ethernet_label_alt': "{wifi_icon} {ip_addr}",
1719
'ethernet_icon': "\ueba9",
1820
'container_padding': {'top': 0, 'left': 0, 'bottom': 0, 'right': 0},
1921
'animation': {
@@ -46,6 +48,14 @@
4648
'required': False
4749
}
4850
},
51+
'ethernet_label': {
52+
'type': 'string',
53+
'default': DEFAULTS['ethernet_label']
54+
},
55+
'ethernet_label_alt': {
56+
'type': 'string',
57+
'default': DEFAULTS['ethernet_label_alt']
58+
},
4959
'ethernet_icon': {
5060
'type': 'string',
5161
'default': DEFAULTS['ethernet_icon']

src/core/widgets/yasb/wifi.py

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from core.utils.widgets.animation_manager import AnimationManager
1010
from core.utils.utilities import add_shadow
1111

12+
1213
class WifiWidget(BaseWidget):
1314
validation_schema = VALIDATION_SCHEMA
1415

@@ -18,6 +19,8 @@ def __init__(
1819
label_alt: str,
1920
update_interval: int,
2021
wifi_icons: list[str],
22+
ethernet_label: str,
23+
ethernet_label_alt: str,
2124
ethernet_icon: str,
2225
animation: dict[str, str],
2326
container_padding: dict[str, int],
@@ -30,16 +33,19 @@ def __init__(
3033
self._ethernet_icon = ethernet_icon
3134

3235
self._show_alt_label = False
36+
self._ethernet_active = False
3337
self._label_content = label
3438
self._label_alt_content = label_alt
39+
self._ethernet_label_content = ethernet_label
40+
self._ethernet_label_alt_content = ethernet_label_alt
3541
self._animation = animation
3642
self._padding = container_padding
3743
self._label_shadow = label_shadow
3844
self._container_shadow = container_shadow
3945
# Construct container
4046
self._widget_container_layout: QHBoxLayout = QHBoxLayout()
4147
self._widget_container_layout.setSpacing(0)
42-
self._widget_container_layout.setContentsMargins(self._padding['left'],self._padding['top'],self._padding['right'],self._padding['bottom'])
48+
self._widget_container_layout.setContentsMargins(self._padding['left'], self._padding['top'], self._padding['right'], self._padding['bottom'])
4349
# Initialize container
4450
self._widget_container: QWidget = QWidget()
4551
self._widget_container.setLayout(self._widget_container_layout)
@@ -50,6 +56,7 @@ def __init__(
5056
self.widget_layout.addWidget(self._widget_container)
5157

5258
self._create_dynamically_label(self._label_content, self._label_alt_content)
59+
self._create_dynamically_label(self._ethernet_label_content, self._ethernet_label_alt_content, is_ethernet=True)
5360

5461
self.register_callback("toggle_label", self._toggle_label)
5562
self.register_callback("update_label", self._update_label)
@@ -61,20 +68,33 @@ def __init__(
6168

6269
self.start_timer()
6370

71+
def _display_correct_label(self):
72+
active_widget_group = "ethernet" if self._ethernet_active else "wifi"
73+
widget_groups = {
74+
"wifi": (self._widgets, self._widgets_alt),
75+
"ethernet": (self._widgets_ethernet, self._widgets_ethernet_alt),
76+
}
77+
for name, (widgets, widgets_alt) in widget_groups.items():
78+
if name == active_widget_group:
79+
for widget in widgets:
80+
widget.setVisible(not self._show_alt_label)
81+
for widget in widgets_alt:
82+
widget.setVisible(self._show_alt_label)
83+
else:
84+
for widget in widgets:
85+
widget.setVisible(False)
86+
for widget in widgets_alt:
87+
widget.setVisible(False)
88+
6489
def _toggle_label(self):
6590
if self._animation['enabled']:
6691
AnimationManager.animate(self, self._animation['type'], self._animation['duration'])
6792
self._show_alt_label = not self._show_alt_label
68-
for widget in self._widgets:
69-
widget.setVisible(not self._show_alt_label)
70-
for widget in self._widgets_alt:
71-
widget.setVisible(self._show_alt_label)
7293
self._update_label()
7394

74-
75-
def _create_dynamically_label(self, content: str, content_alt: str):
76-
def process_content(content, is_alt=False):
77-
label_parts = re.split('(<span.*?>.*?</span>)', content) #Filters out empty parts before entering the loop
95+
def _create_dynamically_label(self, content: str, content_alt: str, is_ethernet=False):
96+
def process_content(content, is_alt=False, is_ethernet=False):
97+
label_parts = re.split('(<span.*?>.*?</span>)', content) # Filters out empty parts before entering the loop
7898
label_parts = [part for part in label_parts if part]
7999
widgets = []
80100
for part in label_parts:
@@ -94,39 +114,48 @@ def process_content(content, is_alt=False):
94114
add_shadow(label, self._label_shadow)
95115
self._widget_container_layout.addWidget(label)
96116
widgets.append(label)
97-
if is_alt:
117+
if is_alt or is_ethernet:
98118
label.hide()
99119
else:
100120
label.show()
101121
return widgets
102-
self._widgets = process_content(content)
103-
self._widgets_alt = process_content(content_alt, is_alt=True)
104122

123+
if is_ethernet:
124+
self._widgets_ethernet = process_content(content, is_ethernet=True)
125+
self._widgets_ethernet_alt = process_content(content_alt, is_alt=True, is_ethernet=True)
126+
else:
127+
self._widgets = process_content(content)
128+
self._widgets_alt = process_content(content_alt, is_alt=True)
105129

106130
def _update_label(self):
107-
active_widgets = self._widgets_alt if self._show_alt_label else self._widgets
108-
active_label_content = self._label_alt_content if self._show_alt_label else self._label_content
109-
label_parts = re.split('(<span.*?>.*?</span>)', active_label_content)
110-
label_parts = [part for part in label_parts if part]
111-
widget_index = 0
112131
try:
113132
connection_info = NetworkInformation.get_internet_connection_profile()
114133
ip_addr = socket.gethostbyname(socket.gethostname())
115-
116-
# If no connection or WiFi connection, check WiFi connection
117-
# (it will set the icon to the 0% icon (no connection) if no WiFi connection is found)
118134
if connection_info is None or connection_info.is_wlan_connection_profile:
119-
# Retrieve WiFi information
135+
self._ethernet_active = False
136+
# sets the wifi_icon to the 0% icon if no WiFi connection is found
120137
wifi_icon, wifi_strength = self._get_wifi_icon()
121138
wifi_name = self._get_wifi_name()
122139
else:
123-
# Otherwise, there is a connection that is Ethernet.
140+
self._ethernet_active = True
124141
wifi_icon = self._ethernet_icon
125142
wifi_name = 'Ethernet'
126143
wifi_strength = 'N/A'
127144
except Exception as e:
128145
logging.error(f'Error in wifi widget update: {e}')
129146
wifi_icon = wifi_name = wifi_strength = "N/A"
147+
148+
self._display_correct_label()
149+
if self._ethernet_active:
150+
active_widgets = self._widgets_ethernet_alt if self._show_alt_label else self._widgets_ethernet
151+
active_label_content = self._ethernet_label_alt_content if self._show_alt_label else self._ethernet_label_content
152+
else:
153+
active_widgets = self._widgets_alt if self._show_alt_label else self._widgets
154+
active_label_content = self._label_alt_content if self._show_alt_label else self._label_content
155+
156+
label_parts = re.split('(<span.*?>.*?</span>)', active_label_content)
157+
label_parts = [part for part in label_parts if part]
158+
widget_index = 0
130159
label_options = {
131160
"{wifi_icon}": wifi_icon,
132161
"{wifi_name}": wifi_name,
@@ -164,7 +193,7 @@ def _get_wifi_name(self):
164193
for connection in connections:
165194
if connection.get_network_connectivity_level() == NetworkConnectivityLevel.INTERNET_ACCESS:
166195
return connection.profile_name
167-
return "No WiFi"
196+
return "Disconnected"
168197

169198
def _get_wifi_icon(self):
170199
strength = self._get_wifi_strength()

0 commit comments

Comments
 (0)