mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Changed: Store app wide night mode in NightMode.APP_NIGHT_MODE so that libraries can use it directly without having to load or get it from termux properties
This commit is contained in:
@@ -50,6 +50,8 @@ import com.termux.app.settings.properties.TermuxAppSharedProperties;
|
||||
import com.termux.shared.termux.interact.TextInputDialogUtils;
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.termux.TermuxUtils;
|
||||
import com.termux.shared.termux.theme.TermuxThemeUtils;
|
||||
import com.termux.shared.theme.NightMode;
|
||||
import com.termux.shared.theme.ThemeUtils;
|
||||
import com.termux.shared.view.ViewUtils;
|
||||
import com.termux.terminal.TerminalSession;
|
||||
@@ -408,7 +410,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
|
||||
|
||||
private void setActivityTheme() {
|
||||
if (ThemeUtils.shouldEnableDarkTheme(this, mProperties.getNightMode())) {
|
||||
// Update NightMode.APP_NIGHT_MODE
|
||||
TermuxThemeUtils.setAppNightMode(mProperties.getNightMode());
|
||||
|
||||
if (ThemeUtils.shouldEnableDarkTheme(this, NightMode.getAppNightMode().getName())) {
|
||||
this.setTheme(R.style.Theme_Termux_Black);
|
||||
} else {
|
||||
this.setTheme(R.style.Theme_Termux);
|
||||
@@ -416,7 +421,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
}
|
||||
|
||||
private void setDrawerTheme() {
|
||||
if (ThemeUtils.shouldEnableDarkTheme(this, mProperties.getNightMode())) {
|
||||
if (ThemeUtils.shouldEnableDarkTheme(this, NightMode.getAppNightMode().getName())) {
|
||||
findViewById(R.id.left_drawer).setBackgroundColor(ContextCompat.getColor(this,
|
||||
android.R.color.background_dark));
|
||||
((ImageButton) findViewById(R.id.settings_button)).setColorFilter(Color.WHITE);
|
||||
@@ -904,13 +909,16 @@ public final class TermuxActivity extends Activity implements ServiceConnection
|
||||
}
|
||||
|
||||
private void reloadActivityStyling() {
|
||||
if (mProperties!= null) {
|
||||
if (mProperties != null) {
|
||||
mProperties.loadTermuxPropertiesFromDisk();
|
||||
|
||||
if (mExtraKeysView != null) {
|
||||
mExtraKeysView.setButtonTextAllCaps(mProperties.shouldExtraKeysTextBeAllCaps());
|
||||
mExtraKeysView.reload(mProperties.getExtraKeysInfo());
|
||||
}
|
||||
|
||||
// Update NightMode.APP_NIGHT_MODE
|
||||
TermuxThemeUtils.setAppNightMode(mProperties.getNightMode());
|
||||
}
|
||||
|
||||
setMargins();
|
||||
|
@@ -7,19 +7,25 @@ import com.termux.shared.termux.TermuxConstants;
|
||||
import com.termux.shared.termux.crash.TermuxCrashUtils;
|
||||
import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences;
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.termux.theme.TermuxThemeUtils;
|
||||
|
||||
|
||||
public class TermuxApplication extends Application {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
Context context = getApplicationContext();
|
||||
|
||||
// Set crash handler for the app
|
||||
TermuxCrashUtils.setCrashHandler(this);
|
||||
|
||||
// Set log config for the app
|
||||
setLogConfig(getApplicationContext());
|
||||
setLogConfig(context);
|
||||
|
||||
Logger.logDebug("Starting Application");
|
||||
|
||||
// Set NightMode.APP_NIGHT_MODE
|
||||
TermuxThemeUtils.setAppNightMode(context);
|
||||
}
|
||||
|
||||
public static void setLogConfig(Context context) {
|
||||
|
@@ -21,6 +21,7 @@ import androidx.core.content.ContextCompat;
|
||||
import com.termux.R;
|
||||
import com.termux.app.TermuxActivity;
|
||||
import com.termux.shared.termux.shell.command.runner.terminal.TermuxSession;
|
||||
import com.termux.shared.theme.NightMode;
|
||||
import com.termux.shared.theme.ThemeUtils;
|
||||
import com.termux.terminal.TerminalSession;
|
||||
|
||||
@@ -56,7 +57,7 @@ public class TermuxSessionsListViewController extends ArrayAdapter<TermuxSession
|
||||
return sessionRowView;
|
||||
}
|
||||
|
||||
boolean shouldEnableDarkTheme = ThemeUtils.shouldEnableDarkTheme(mActivity, mActivity.getProperties().getNightMode());
|
||||
boolean shouldEnableDarkTheme = ThemeUtils.shouldEnableDarkTheme(mActivity, NightMode.getAppNightMode().getName());
|
||||
|
||||
if (shouldEnableDarkTheme) {
|
||||
sessionTitleView.setBackground(
|
||||
|
@@ -0,0 +1,25 @@
|
||||
package com.termux.shared.termux.theme;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.termux.shared.termux.settings.properties.TermuxPropertyConstants;
|
||||
import com.termux.shared.termux.settings.properties.TermuxSharedProperties;
|
||||
import com.termux.shared.theme.NightMode;
|
||||
|
||||
public class TermuxThemeUtils {
|
||||
|
||||
/** Get the {@link TermuxPropertyConstants#KEY_NIGHT_MODE} value from the properties file on disk
|
||||
* and set it to app wide night mode value. */
|
||||
public static void setAppNightMode(@NonNull Context context) {
|
||||
NightMode.setAppNightMode(TermuxSharedProperties.getNightMode(context));
|
||||
}
|
||||
|
||||
/** Set name as app wide night mode value. */
|
||||
public static void setAppNightMode(@Nullable String name) {
|
||||
NightMode.setAppNightMode(name);
|
||||
}
|
||||
|
||||
}
|
@@ -1,7 +1,11 @@
|
||||
package com.termux.shared.theme;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
|
||||
import com.termux.shared.logger.Logger;
|
||||
|
||||
/** The modes used by to decide night mode for themes. */
|
||||
public enum NightMode {
|
||||
|
||||
@@ -17,6 +21,11 @@ public enum NightMode {
|
||||
*/
|
||||
SYSTEM("system", AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
|
||||
|
||||
/** The current app wide night mode used by various libraries. Defaults to {@link #SYSTEM}. */
|
||||
private static NightMode APP_NIGHT_MODE;
|
||||
|
||||
private static final String LOG_TAG = "NightMode";
|
||||
|
||||
private final String name;
|
||||
private final int mode;
|
||||
|
||||
@@ -33,16 +42,50 @@ public enum NightMode {
|
||||
return mode;
|
||||
}
|
||||
|
||||
public static Integer modeOf(String name) {
|
||||
if (TRUE.name.equals(name))
|
||||
return TRUE.mode;
|
||||
else if (FALSE.name.equals(name))
|
||||
return FALSE.mode;
|
||||
else if (SYSTEM.name.equals(name)) {
|
||||
return SYSTEM.mode;
|
||||
} else {
|
||||
return null;
|
||||
|
||||
/** Get {@link NightMode} for {@code name} if found, otherwise {@code null}. */
|
||||
@Nullable
|
||||
public static NightMode modeOf(String name) {
|
||||
for (NightMode v : NightMode.values()) {
|
||||
if (v.name.equals(name)) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Get {@link NightMode} for {@code name} if found, otherwise {@code def}. */
|
||||
@NonNull
|
||||
public static NightMode modeOf(@Nullable String name, @NonNull NightMode def) {
|
||||
NightMode nightMode = modeOf(name);
|
||||
return nightMode != null ? nightMode : def;
|
||||
}
|
||||
|
||||
|
||||
/** Set {@link #APP_NIGHT_MODE}. */
|
||||
public static void setAppNightMode(@Nullable String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
APP_NIGHT_MODE = SYSTEM;
|
||||
} else {
|
||||
NightMode nightMode = NightMode.modeOf(name);
|
||||
if (nightMode == null) {
|
||||
Logger.logError(LOG_TAG, "Invalid APP_NIGHT_MODE \"" + name + "\"");
|
||||
return;
|
||||
}
|
||||
APP_NIGHT_MODE = nightMode;
|
||||
}
|
||||
|
||||
Logger.logVerbose(LOG_TAG, "Set APP_NIGHT_MODE to \"" + APP_NIGHT_MODE.getName() + "\"");
|
||||
}
|
||||
|
||||
/** Get {@link #APP_NIGHT_MODE}. */
|
||||
@NonNull
|
||||
public static NightMode getAppNightMode() {
|
||||
if (APP_NIGHT_MODE == null)
|
||||
APP_NIGHT_MODE = SYSTEM;
|
||||
|
||||
return APP_NIGHT_MODE;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user