Merge pull request #2013 from webmin/dev/theme-switcher

Add theme switcher using hotkeys
This commit is contained in:
Jamie Cameron
2023-10-01 15:21:47 -07:00
committed by GitHub
9 changed files with 113 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@@ -96,6 +96,7 @@ if ($current_lang_info->{'rtl'} || $current_lang eq "ar") {
# Page header
print "<html>\n";
print "<head>\n";
print &ui_switch_theme_javascript();
print "<title>$title</title>\n";
my $imgdir = "@{[&get_webprefix()]}/images";
my $prod = 'webmin';

View File

@@ -84,7 +84,7 @@ if (@has > 1) {
}
print "</div>";
}
print &ui_switch_theme_javascript();
print "<div class='wrapper leftmenu'>\n";
print "<table id='main' width='100%'><tbody><tr><td>\n";

File diff suppressed because one or more lines are too long

View File

@@ -60,6 +60,7 @@ $vers || usage();
"webmin-search-lib.pl", "WebminCore.pm",
"record-login.pl", "record-logout.pl", "record-failed.pl",
"robots.txt", "unauthenticated", "bin", "html-editor-lib.pl",
"switch_theme.cgi",
);
if ($min) {
# Only those required by others

45
switch_theme.cgi Executable file
View File

@@ -0,0 +1,45 @@
#!/usr/local/bin/perl
# switch_theme.cgi
# Change the theme for the current user if allowed
BEGIN { push(@INC, "."); };
use WebminCore;
&init_config();
&ReadParse();
my $err = sub {
&PrintHeader();
print("<tt>Cannot change theme : $_[0]</tt>\n");
exit(1);
};
# Check if in debug mode
&$err("Debug mode is not enabled!")
if (!$gconfig{'debug_enabled'} &&
!$gconfig{'debug_theme_switcher'});
# Check if allowed to change theme,
# otherwise throw an error
if (!&foreign_available('theme') &&
!&foreign_available('change-user') &&
!&foreign_available('webmin') &&
!&foreign_available('acl')) {
&$err("You are not allowed to change themes!");
}
# Check if the theme is known
&$err("Theme identification is not known!")
if (!$in{'theme'} || $in{'theme'} !~ /^[123]$/);
# Check if the remote user is known
&$err("Remote user is not known!") if (!$remote_user);
# Define the theme
my $themes = {
'1' => 'authentic-theme',
'2' => 'gray-theme',
'3' => '' };
my $theme = $themes->{$in{'theme'}};
# Change the theme
&foreign_require('acl');
my @users = &acl::list_users();
my ($user) = grep { $_->{'name'} eq $remote_user } @users;
$user->{'theme'} = $theme;
&acl::modify_user($user->{'name'}, $user);
&restart_miniserv();
&redirect(&get_webprefix() . "/");

View File

@@ -2280,6 +2280,31 @@ for(var i=0; i<values.length; i++) {
EOF
}
=head2 ui_switch_theme_javascript()
The subroutine is designed to load JavaScript
for switching themes using hotkeys.
Hotkeys are:
To activate theme switch mode:
Use: `Ctrl+Alt+T` (or `Control+Option+T` on Mac).
Immediately after, within 1 second, select your desired theme by pressing:
- `Shift + A`: Authentic theme
- `Shift + G`: Gray theme
- `Shift + L`: Legacy theme.
=cut
sub ui_switch_theme_javascript
{
return &theme_ui_switch_theme_javascript(@_) if (defined(&theme_ui_switch_theme_javascript));
return "" if (!$gconfig{'debug_enabled'} && !$gconfig{'debug_theme_switcher'});
my $switch_script = "<script>const __webmin_webprefix__ = '@{[&get_webprefix()]}';</script>\n";
$switch_script .= "<script type=\"text/javascript\" src=\"@{[&get_webprefix()]}/unauthenticated/switch_theme.js?@{[&get_webmin_version(1)]}\"></script>\n";
return $switch_script;
}
####################### grid layout functions
=head2 ui_grid_table(&elements, columns, [width-percent], [&tds], [tabletags], [title])

View File

@@ -0,0 +1,31 @@
(function () {
let firstCombinationPressed = false;
document.addEventListener("keydown", function (event) {
// Check for Ctrl+Alt+T or Control+Option+T
if (event.ctrlKey && event.altKey && event.keyCode === 84) {
firstCombinationPressed = true;
// Set a timeout to reset the state after a short period (e.g., 1 seconds)
setTimeout(() => {
firstCombinationPressed = false;
}, 1000);
}
if (firstCombinationPressed && event.shiftKey &&
(event.keyCode === 65 || event.keyCode === 71 || event.keyCode === 76)) {
const theme =
// Shift + A : Authentic theme
event.keyCode === 65 ? 1 :
// Shift + G : Gray theme
event.keyCode === 71 ? 2 :
// Shift + L : Legacy theme.
event.keyCode === 76 ? 3 : null;
firstCombinationPressed = false;
try {
top.document.documentElement.style.filter = 'grayscale(100%) blur(0.5px) brightness(0.75) opacity(0.5)';
top.document.documentElement.style.cursor = 'wait';
top.document.documentElement.style.pointerEvents = 'none';
} catch (error) {}
top.location.href = __webmin_webprefix__ + "/switch_theme.cgi?theme=" + theme + "";
}
});
})();

View File

@@ -1119,6 +1119,7 @@ my $text = defined($tconfig{'cs_text'}) ? $tconfig{'cs_text'} :
my $bgimage = defined($tconfig{'bgimage'}) ? "background=$tconfig{'bgimage'}" : "";
my $dir = $current_lang_info->{'dir'} ? "dir=\"$current_lang_info->{'dir'}\"" : "";
my $html_body = "<body bgcolor=\"#$bgcolor\" link=\"#$link\" vlink=\"#$link\" text=\"#$text\" style=\"height:100%\" $bgimage $tconfig{'inbody'} $dir $_[8]>\n";
print &ui_switch_theme_javascript();
$html_body =~ s/\s+\>/>/g;
print $html_body;
print "<script>function _document_cookie_set_client_height(){document.cookie='client_height='+document.documentElement.clientHeight+'';}_document_cookie_set_client_height();window.onresize=_document_cookie_set_client_height</script>\n";
@@ -4449,10 +4450,16 @@ Returns the version of Webmin currently being run, such as 1.450.
=cut
sub get_webmin_version
{
my ($str) = @_;
if (!$get_webmin_version) {
$get_webmin_version = &read_file_contents("$root_directory/version");
$get_webmin_version =~ s/\r|\n//g;
}
if ($str) {
my $get_webmin_version_str = $get_webmin_version;
$get_webmin_version_str =~ s/\.//g;
return $get_webmin_version_str;
}
return $get_webmin_version;
}