Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ex-theme into develop
  • Loading branch information
felixhahnweilheim committed Dec 13, 2023
2 parents f070bdd + 441fffa commit 6c20ea0
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: felixhahnweilheim
custom: https://paypal.me/felixhahnharp
4 changes: 0 additions & 4 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace humhub\modules\flexTheme;

use humhub\modules\ui\icon\widgets\Icon;
use Yii;
use yii\helpers\Url;

class Events
{
public static function onConsoleInit($event)
Expand Down
7 changes: 7 additions & 0 deletions controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use humhub\modules\flexTheme\models\Config;
use humhub\modules\flexTheme\models\ColorSettings;
use humhub\modules\flexTheme\models\DarkMode;
use humhub\modules\flexTheme\models\AdvancedSettings;
use Yii;

Expand Down Expand Up @@ -47,12 +48,14 @@ public function actionAdvanced()
$form = new AdvancedSettings();
$config = new Config();
$colorSettings = new ColorSettings();
$darkModeSettings = new DarkMode();

if(!empty(Yii::$app->request->post())) {
$data = json_decode(Yii::$app->request->post()['AdvancedSettings']['settingsJson'], true);

$config->load($data);
$colorSettings->load($data);
$darkModeSettings->load($data);

// Check validation before saving anything
if (!$config->validate()) {
Expand All @@ -63,6 +66,10 @@ public function actionAdvanced()
$form->addError('settingsJson', Yii::t('FlexThemeModule.admin', 'There seem to be invalid values!') . ' (ColorSettings)');
return $this->render('advanced', ['model' => $form]);
}
if (!$darkModeSettings->validate()) {
$form->addError('settingsJson', Yii::t('FlexThemeModule.admin', 'There seem to be invalid values!') . ' (DarkModeSettings)');
return $this->render('advanced', ['model' => $form]);
}
// Save
if ($config->save() && $colorSettings->save()) {
$this->view->saved();
Expand Down
1 change: 0 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
- New option: show file upload options as buttons instead of dropdown)
- New controller action: flex-theme/rebuild - only for developping!
- Enh: various code style improvements

## 0.2.1 (22 may 2023)
- Fix: save likeIconColor
- Enh: Import/Export (JSON)
Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Please note: This still needs more testing and some colors that are hard-coded i

[Details for Developpers](https://github.com/felixhahnweilheim/humhub-flex-theme/blob/main/themes/FlexTheme/less/README.md)

### Dark Mode (experimental)
The dark mode is disabled by default because it is still experimental.
You should define all colors for it to work properly. There is no dark mode button because the theme uses your browser's dark mode.

### Social Controls

**Comment Link**
Expand Down
20 changes: 19 additions & 1 deletion models/AdvancedSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use humhub\modules\flexTheme\models\Config;
use humhub\modules\flexTheme\models\ColorSettings;
use humhub\modules\flexTheme\models\DarkMode;

use Yii;

Expand Down Expand Up @@ -62,6 +63,7 @@ protected function getSettingsArray(): array
{
$config = [];
$colors = [];
$darkMode = [];

$module = Yii::$app->getModule('flex-theme');

Expand All @@ -87,6 +89,22 @@ protected function getSettingsArray(): array
}
}

return ['Config' => $config, 'ColorSettings' => $colors];
// Get Dark Mode settings
$darkModeEnabled = $module->settings->get('darkModeEnabled');
if (!empty($darkModeEnabled)) {
$darkMode['darkModeEnabled'] = (bool) $darkModeEnabled;
}
// Get dark colors
$color_names = array_merge(DarkMode::MAIN_COLORS, DarkMode::TEXT_COLORS, DarkMode::BACKGROUND_COLORS);
foreach( $color_names as $color ) {
$value = $module->settings->get('dark_' . $color);

// exclude empty settings
if(!empty($value)) {
$darkMode[$color] = $value;
}
}

return ['Config' => $config, 'ColorSettings' => $colors, 'DarkMode' => $darkMode];
}
}
245 changes: 245 additions & 0 deletions models/DarkMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<?php

namespace humhub\modules\flexTheme\models;

use Yii;
use humhub\modules\flexTheme\helpers\ColorHelper;
use humhub\modules\ui\view\helpers\ThemeHelper;
use humhub\modules\ui\icon\widgets\Icon;

class DarkMode extends \yii\base\Model
{
/*
* Dark Mode disabled by default
*/
public $darkModeEnabled;

// Main Colors (configurable)
const MAIN_COLORS = ['default', 'primary', 'info', 'link', 'success', 'warning', 'danger'];
public $default;
public $primary;
public $info;
public $link;
public $success;
public $warning;
public $danger;

// Text Colors (configurable)
const TEXT_COLORS = ['text_color_main', 'text_color_secondary', 'text_color_highlight', 'text_color_soft', 'text_color_soft2', 'text_color_soft3', 'text_color_contrast'];
public $text_color_main;
public $text_color_secondary;
public $text_color_highlight;
public $text_color_soft;
public $text_color_soft2;
public $text_color_soft3;
public $text_color_contrast;

// Background Colors (configurable)
const BACKGROUND_COLORS = ['background_color_main', 'background_color_secondary', 'background_color_page', 'background_color_highlight', 'background_color_highlight_soft', 'background3', 'background4'];
public $background_color_main;
public $background_color_secondary;
public $background_color_page;
public $background_color_highlight;
public $background_color_highlight_soft;
public $background3;
public $background4;

// Special colors which were generated by Less functions lighten(), darken() or fade()
const SPECIAL_COLORS = ['default__darken__2','default__darken__5','default__lighten__2','primary__darken__5','primary__darken__10','primary__lighten__5','primary__lighten__8','primary__lighten__10','primary__lighten__20','primary__lighten__25','info__darken__5','info__darken__10','info__lighten__5','info__lighten__8','info__lighten__25','info__lighten__45','info__lighten__50','danger__darken__5','danger__darken__10','danger__lighten__5','danger__lighten__20','success__darken__5','success__darken__10','success__lighten__5','success__lighten__20','warning__darken__2','warning__darken__5','warning__darken__10','warning__lighten__5','warning__lighten__20','link__darken__2','link__lighten__5','background_color_secondary__darken__5','background_color_page__lighten__3','background_color_page__darken__5','background_color_page__darken__8','text_color_secondary__lighten__25','link__fade__60'];
public $default__darken__2;
public $default__darken__5;
public $default__lighten__2;
public $primary__darken__5;
public $primary__darken__10;
public $primary__lighten__5;
public $primary__lighten__8;
public $primary__lighten__10;
public $primary__lighten__20;
public $primary__lighten__25;
public $info__darken__5;
public $info__darken__10;
public $info__lighten__5;
public $info__lighten__8;
public $info__lighten__25;
public $info__lighten__45;
public $info__lighten__50;
public $danger__darken__5;
public $danger__darken__10;
public $danger__lighten__5;
public $danger__lighten__20;
public $success__darken__5;
public $success__darken__10;
public $success__lighten__5;
public $success__lighten__20;
public $warning__darken__2;
public $warning__darken__5;
public $warning__darken__10;
public $warning__lighten__5;
public $warning__lighten__20;
public $link__darken__2;
public $link__lighten__5;
public $background_color_secondary__darken__5;
public $background_color_page__lighten__3;
public $background_color_page__darken__5;
public $background_color_page__darken__8;
public $text_color_secondary__lighten__25;
public $link__fade__60;

public static function isEnabled()
{
return (bool) Yii::$app->getModule('flex-theme')->settings->get('darkModeEnabled');
}

public static function getColors()
{
$result = [];

$module = Yii::$app->getModule('flex-theme');
$all_colors = array_merge(self::MAIN_COLORS, self::TEXT_COLORS, self::BACKGROUND_COLORS, self::SPECIAL_COLORS);

foreach ($all_colors as $color) {
$value = $module->settings->get('dark_' . $color);
// Exclude empty colors (fallback: light theme)
if (!empty($value)) {
$result[$color] = $value;
}
}

return $result;
}

public function init()
{
parent::init();

$settings = Yii::$app->getModule('flex-theme')->settings;

$this->darkModeEnabled = $settings->get('darkModeEnabled', false);

$configurable_colors = array_merge(self::MAIN_COLORS, self::TEXT_COLORS, self::BACKGROUND_COLORS);

foreach($configurable_colors as $color) {
$this->$color = $settings->get('dark_' . $color);
}
}

public function attributeLabels()
{
$labels['darkModeEnabled'] = Yii::t('FlexThemeModule.admin', 'Enable Dark Mode') . Yii::t('FlexThemeModule.admin', ' (experimental)');

return $labels;
}

public function attributeHints()
{
$hints = [];

$hints['darkModeEnabled'] = Yii::t('FlexThemeModule.admin', 'Dark Mode will only be active when the user\'s browser is in dark mode.') . '<br/>' .
'<strong>' . Yii::t('FlexThemeModule.admin', 'Note') . ':</strong> ' . Yii::t('FlexThemeModule.admin', 'Please define all colors below, at least text and Background colors.');

$base_theme = ThemeHelper::getThemeByName('HumHub');
$configurable_colors = array_merge(self::MAIN_COLORS, self::TEXT_COLORS, self::BACKGROUND_COLORS);

foreach ($configurable_colors as $color) {
$theme_var = str_replace('_', '-', $color);
$default_value = $base_theme->variable($theme_var);
$icon = Icon::get('circle', ['color' => $default_value ]);
$hints[$color] = Yii::t('FlexThemeModule.admin', 'Default (Light Theme)') . ': ' . '<code>' . $default_value . '</code> ' . $icon;
}

return $hints;
}

public function rules()
{
return [
[['darkModeEnabled'], 'boolean'],
[['default', 'primary', 'info', 'link', 'success', 'warning', 'danger',
'text_color_main', 'text_color_secondary', 'text_color_highlight', 'text_color_soft', 'text_color_soft2', 'text_color_soft3', 'text_color_contrast',
'background_color_main', 'background_color_secondary', 'background_color_page', 'background_color_highlight', 'background_color_highlight_soft', 'background3', 'background4'
], 'validateHexColor']
];
}

public function validateHexColor($attribute, $params, $validator)
{
if (!preg_match("/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/", $this->$attribute)) {
$this->addError($attribute, Yii::t('FlexThemeModule.admin', 'Invalid Format') . '. ' . Yii::t('FlexThemeModule.admin', 'Must be a color in hexadecimal format, like "#00aaff" or "#FA0"'));
}
}

public function save()
{
if(!$this->validate()) {
return false;
}

Yii::$app->getModule('flex-theme')->settings->set('darkModeEnabled', $this->darkModeEnabled);

// Save color values
self::saveColors();

// Calculate and save lightened, darkened and faded colors
self::saveSpecialColors();

return true;
}

public function saveColors()
{
$settings = Yii::$app->getModule('flex-theme')->settings;
$configurable_colors = array_merge(self::MAIN_COLORS, self::TEXT_COLORS, self::BACKGROUND_COLORS);

foreach ($configurable_colors as $color) {

$value = $this->$color;

// Save as module settings (value can be emtpy)
$settings->set('dark_' . $color, $value);
}
}

public function saveSpecialColors()
{
$module = Yii::$app->getModule('flex-theme');

$special_colors = self::SPECIAL_COLORS;

foreach ($special_colors as $color) {

// split color names into base color, manipulation function and amount of manipulation
list($base_var, $function, $amount) = explode("__", $color);

// Get value of base color
$original_color = $this->$base_var;
// Special colors should be empty if base color is not defined (colors of the light mode will be used)
if (empty($original_color)) {

$value = '';

// if base color is defined calculate color value with ColorHelper functions
} elseif ($function == 'darken') {

$value = ColorHelper::darken($original_color, $amount);

} elseif ($function == 'lighten') {

$value = ColorHelper::lighten($original_color, $amount);

} elseif ($function == 'fade') {

$value = ColorHelper::fade($original_color, $amount);

} elseif ($function == 'fadeout') {

$value = ColorHelper::fadeout($original_color, $amount);

} else {
$value = '';
}

// Save calculated value
$module->settings->set('dark_' . $color, $value);
}
}
}

0 comments on commit 6c20ea0

Please sign in to comment.