Skip to content

Commit 700297c

Browse files
author
spaceAngel
committed
[gui][daemon] Support for icon check enabled function (e.g. icon enabled only if window active #feature
1 parent de94e5a commit 700297c

9 files changed

Lines changed: 93 additions & 4 deletions

File tree

gui/css/mainPanel/macros.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
.notification:empty {
2828
display:none;
2929
}
30+
31+
.disabled {
32+
opacity: 0.4;
33+
}
3034
}

gui/js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ document.addEventListener('DOMContentLoaded', function() {
7070
function() {socket.send('loadmacros', 123);},
7171
2000
7272
);
73+
macrosWidget.init();
7374

7475
lockScreenImage.init();
7576

gui/js/widgets/macros.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
/* global cyberPanel, mixins, socket */
22
var macrosWidget = {
3+
enableStates: [],
4+
35
handle: function(data) {
46
cyberPanel.macros = data.macros;
7+
},
8+
9+
init: function() {
10+
socket.registerHandler('macros.enabled', macrosWidget.handleEnabled);
11+
setInterval(
12+
function() {socket.send('macros.enabled', 123);},
13+
1500
14+
);
15+
},
16+
17+
handleEnabled: function(data) {
18+
macrosWidget.enableStates = data.enabled;
519
}
620
};
721

822
mixins.push({
923
methods: {
1024
runMacro: function(hash) {
11-
socket.send('macro', [hash]);
25+
if (
26+
macrosWidget.enableStates == null
27+
|| macrosWidget.enableStates[hash] == true
28+
) {
29+
socket.send('macro', [hash]);
30+
}
1231
}
1332
}
1433
});

gui/snippets/mainpanel/macros.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<template v-for="macro in macros">
2-
<div class="icon" data-hash="" v-on:click="runMacro(macro.hash)" v-if="!macro.isDelimiter" v-bind:style="'float:' +macro.position">
2+
<div
3+
class="icon"
4+
v-bind:class="{disabled: macrosWidget.enableStates != null && macrosWidget.enableStates[macro.hash] != true}"
5+
data-hash=""
6+
v-on:click="runMacro(macro.hash)"
7+
v-if="!macro.isDelimiter"
8+
v-bind:style="'float:' +macro.position"
9+
>
310
<span v-if="macro.notification" class="notification"><% eval(macro.notification)%></span>
411
<i class="fa fa-4x" v-bind:class="[macro.icon]" v-if="macro.icon"></i>
512
<img v-if="macro.iconImage" v-bind:src="'data:' +macro.iconImage" style="border-color:1px solid blue;"/>

src/CyberPanel/Commands/CommandResolver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use CyberPanel\Commands\Commands\StorageCommand;
1818
use CyberPanel\Macros\Commands\LoadMacrosCommand;
1919
use CyberPanel\Macros\Commands\RunMacroCommand;
20+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
21+
use CyberPanel\Macros\Commands\MacrosEnabledListCommand;
2022

2123
class CommandResolver {
2224

@@ -28,8 +30,6 @@ private function __construct() {
2830
$this->registerCommand('datetime', DateTimeCommand::class);
2931
$this->registerCommand('systeminfo', SystemInfoCommand::class);
3032
$this->registerCommand('hwinfo', HwInfoCommand::class);
31-
$this->registerCommand('loadmacros', LoadMacrosCommand::class);
32-
$this->registerCommand('macro', RunMacroCommand::class);
3333
$this->registerCommand('keyboard', KeyboardCommand::class);
3434
$this->registerCommand('media', MediaCommand::class);
3535
$this->registerCommand('lockscreenimage', LockScreenImageCommand::class);
@@ -39,6 +39,9 @@ private function __construct() {
3939
$this->registerCommand('files', FileManagerCommand::class);
4040
$this->registerCommand('upsstatus', UpsStateCommand::class);
4141
$this->registerCommand('storage', StorageCommand::class);
42+
$this->registerCommand('loadmacros', LoadMacrosCommand::class);
43+
$this->registerCommand('macro', RunMacroCommand::class);
44+
$this->registerCommand('macros.enabled', MacrosEnabledListCommand::class);
4245
}
4346

4447
public static function getInstance() : self {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace CyberPanel\Macros\Commands;
4+
5+
use CyberPanel\Commands\BaseCommand;
6+
use CyberPanel\Storage;
7+
8+
class MacrosEnabledListCommand extends BaseCommand {
9+
public function run() : array {
10+
return ['enabled' => Storage::getInstance()->get('macros.enabled')];
11+
}
12+
}

src/CyberPanel/Macros/Macro.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Macro {
2020

2121
private ?string $notification = NULL;
2222

23+
private $checkEnabledFunction = NULL;
24+
2325
public function getIcon() {
2426
return $this->icon;
2527
}
@@ -95,4 +97,13 @@ public function setNotification(string $notification) : self {
9597
public function getNotification() : ?string {
9698
return $this->notification;
9799
}
100+
101+
public function setCheckEnabledFunction(callable $checkEnabledFunction) : self {
102+
$this->checkEnabledFunction = $checkEnabledFunction;
103+
return $this;
104+
}
105+
106+
public function getCheckEnabledFunction() : ?callable {
107+
return $this->checkEnabledFunction;
108+
}
98109
}

src/CyberPanel/Macros/MacroHandlingService.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
namespace CyberPanel\Macros;
44

55
use CyberPanel\System\Executer;
6+
use CyberPanel\Utils\Traits\HasSocketClient;
67

78
class MacroHandlingService {
89

10+
use HasSocketClient;
11+
912
protected const TICK_INTERVAL = 0.4;
1013

14+
protected const TICK_CHECK_ENABLE = 3000;
15+
1116
protected string $pipefile;
1217

1318
protected static self $instance;
@@ -31,12 +36,18 @@ public function runExecuter() : void {
3136
}
3237

3338
protected function runLoop() : void {
39+
$i = 0;
3440
while (TRUE) {
3541
if (file_exists($this->pipefile)) {
3642
Executer::exec(
3743
'/bin/bash < ' . $this->pipefile . '; rm ' . $this->pipefile
3844
);
3945
}
46+
if ($i % self::TICK_CHECK_ENABLE == 0) {
47+
$this->checkMacrosEnabled();
48+
$i = 0;
49+
}
50+
$i++;
4051
sleep(self::TICK_INTERVAL);
4152
}
4253
}
@@ -47,5 +58,23 @@ public function execCommand(string $command) : void {
4758
fclose($pipe);
4859
}
4960

61+
protected function checkMacrosEnabled() : void {
62+
$rslt = [];
63+
foreach (MacroManager::getInstance()->getMacros() as $key => $macro) {
64+
if (is_callable($macro->getCheckEnabledFunction())) {
65+
$rslt[$key] = call_user_func($macro->getCheckEnabledFunction());
66+
} else {
67+
$rslt[$key] = TRUE;
68+
}
69+
}
70+
$this->sendToSocketServer([
71+
'command' => 'storage',
72+
'parameters' => [
73+
'key' => 'macros.enabled',
74+
'value' => $rslt,
75+
]
76+
]);
77+
}
78+
5079

5180
}

src/CyberPanel/Macros/MacroParser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function parse(array $data) : Macro {
3838
if (array_key_exists('notification', $data)) {
3939
$macro->setNotification($data['notification']);
4040
}
41+
if (array_key_exists('checkEnabledFunction', $data)) {
42+
$macro->setCheckEnabledFunction($data['checkEnabledFunction']);
43+
}
4144
}
4245

4346
return $macro;

0 commit comments

Comments
 (0)