Skip to content

Commit

Permalink
Merge pull request #58 from CogTool-Modern/feature/dark-mode
Browse files Browse the repository at this point in the history
macOS Dark Mode
  • Loading branch information
justingeeslin authored Aug 21, 2020
2 parents 6528ad9 + 329d952 commit 83a01fc
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
1 change: 1 addition & 0 deletions java/edu/cmu/cs/hcii/cogtool/ui/ProjectInteraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ public StartUpDialog(Shell parentWin)
super(parentWin,
L10N.get("CGTL.StartUp", "CogTool Start-up"),
OSUtils.MACOSX ? SWT.MODELESS : SWT.PRIMARY_MODAL);
OSUtils.getPlatform().setTheme(WindowUtil.GLOBAL_DISPLAY);
}

/**
Expand Down
35 changes: 29 additions & 6 deletions java/edu/cmu/cs/hcii/cogtool/uimodel/DefaultScriptUIModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import edu.cmu.cs.hcii.cogtool.util.SWTStringUtil;
import edu.cmu.cs.hcii.cogtool.util.StringUtil;
import edu.cmu.cs.hcii.cogtool.view.SWTListMultiColumn;
import edu.cmu.cs.hcii.cogtool.util.OSUtils;

public class DefaultScriptUIModel extends DefaultUIModel
{
Expand Down Expand Up @@ -142,11 +143,16 @@ protected static String computeActionString(AScriptStep ss,
protected static final Color autoInsertedScriptStepColor =
new Color(null, 229, 229, 229);

protected static final Color autoInsertedScriptStepDarkColor =
new Color(null, 57, 57, 57); // Dark Gray

/**
* This does not need to be disposed since it's a static.
*/
protected static final Color generatedScriptStepColor =
new Color(null, 255, 255, 204);
protected static final Color generatedScriptStepDarkColor =
new Color(null, 80, 80, 15); // Dark tan

/**
* Do not dispose since it is a static.
Expand All @@ -155,6 +161,8 @@ protected static String computeActionString(AScriptStep ss,
*/
protected static final Color obsoletingScriptStepColor =
new Color(null, 153, 204, 255);
protected static final Color obsoletingScriptStepDarkColor =
new Color(null, 50, 79, 108); // Dark powder blue

/**
* This color does not need to be disposed since it's static.
Expand All @@ -164,6 +172,8 @@ protected static String computeActionString(AScriptStep ss,
*/
protected static final Color invalidScriptStepColor =
new Color(null, 255, 102, 0);
protected static final Color invalidScriptStepDarkColor =
new Color(null, 115, 47, 1);

public static class RenderScriptTable extends SWTListMultiColumn
{
Expand Down Expand Up @@ -283,31 +293,44 @@ protected Color getRowBackground(TableItem item)

Color c = null;

Color invalidColor = invalidScriptStepColor;
Color obsoletingColor = obsoletingScriptStepColor;
Color autoInsertedColor = autoInsertedScriptStepColor;
Color generatedColor = generatedScriptStepColor;

// Assign Dark Mode color variants. a11y ✅
if (OSUtils.getPlatform().isSystemDarkAppearance()) {
invalidColor = invalidScriptStepDarkColor;
obsoletingColor = obsoletingScriptStepDarkColor;
autoInsertedColor = autoInsertedScriptStepDarkColor;
generatedColor = generatedScriptStepDarkColor;
}

if (itemData instanceof DefaultModelGeneratorState) {
AScriptStep ss =
((DefaultModelGeneratorState) itemData).getScriptStep();
AScriptStep owner = ss.getOwner();

if (isInvalid || owner.isInvalid()) {
return invalidScriptStepColor;
return invalidColor;
}

if (isObsoleting || owner.isObsolete()) {
return obsoletingScriptStepColor;
return obsoletingColor;
}

if (! ss.isInsertedByUser()) {
if (ss.isDemonstrated()) {
return autoInsertedScriptStepColor;
return autoInsertedColor;
}

return generatedScriptStepColor;
return generatedColor;
}
}
else if (itemData instanceof Frame) {
return isInvalid
? invalidScriptStepColor
: (isObsoleting ? obsoletingScriptStepColor
? invalidColor
: (isObsoleting ? obsoletingColor
: null);
}

Expand Down
20 changes: 8 additions & 12 deletions java/edu/cmu/cs/hcii/cogtool/uimodel/ProjectUIModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
import edu.cmu.cs.hcii.cogtool.util.SWTStringUtil;
import edu.cmu.cs.hcii.cogtool.util.StringUtil;
import edu.cmu.cs.hcii.cogtool.util.WindowUtil;
import edu.cmu.cs.hcii.cogtool.util.OSUtils;

public class ProjectUIModel extends DefaultUIModel
{
Expand Down Expand Up @@ -160,20 +161,16 @@ public DesignReordering(ProjectUIModel projectUIModel)
}
}

protected static Color selectedTextColor =
WindowUtil.GLOBAL_DISPLAY.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT);
protected static Color selectedTextColor = null;

protected static Color unselectedTextColor =
WindowUtil.GLOBAL_DISPLAY.getSystemColor(SWT.COLOR_LIST_FOREGROUND);
protected static Color unselectedTextColor = null;

protected static Color selectedBackgroundColor =
WindowUtil.GLOBAL_DISPLAY.getSystemColor(SWT.COLOR_LIST_SELECTION);

public static Color unselectedTaskBackgroundColor =
WindowUtil.GLOBAL_DISPLAY.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
public static Color unselectedTaskBackgroundColor = null;

protected static Color unselectedGroupBackgroundColor =
new Color(WindowUtil.GLOBAL_DISPLAY, 255, 255, 204);
protected static Color unselectedGroupBackgroundColor = new Color(null, 80, 80, 15);

protected Tree tree;
protected TreeRowHook rowHook = null;
Expand Down Expand Up @@ -283,10 +280,9 @@ public void handleEvent(Event evt)
}
};

public static final Color SELECTED_CELL_COLOR =
new Color(WindowUtil.GLOBAL_DISPLAY, 135, 180, 235);

public static final RGB SELECTED_CELL_RGB = new RGB(20, 120, 200);
public static final Color SELECTED_CELL_COLOR = OSUtils.getPlatform().isSystemDarkAppearance() ? new Color(null, 50, 79, 108): new Color(null, 20, 120, 200);

public static final RGB SELECTED_CELL_RGB = OSUtils.getPlatform().isSystemDarkAppearance() ? new RGB(50, 79, 108) : new RGB(20, 120, 200);

public static final ImageData SELECTED_CELL_IMAGEDATA =
new ImageData(1, 1, 1,
Expand Down
5 changes: 5 additions & 0 deletions java/edu/cmu/cs/hcii/cogtool/util/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public interface PlatformMenuActions
public void doExitApplication();
}


public void setTheme(Display d);

public boolean isSystemDarkAppearance();

/**
* Bind the above callbacks to the Application Menu; only relevant
* for Macintosh, other platforms should ignore this.
Expand Down
10 changes: 10 additions & 0 deletions java/edu/cmu/cs/hcii/cogtool/util/PlatformAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@

public abstract class PlatformAdapter implements Platform
{
public void setTheme(Display d)
{
// do nothing unless overriden
}

public void initPlatformMenu(Display d, PlatformMenuActions a)
{
// do nothing unless overriden
Expand All @@ -105,4 +110,9 @@ public void initTransferData(TransferData td)
{
// do nothing unless overriden
}

public boolean isSystemDarkAppearance() {
// Most OS do not have a dark mode at the time of this writing.
return false;
}
}
Binary file modified lib/macintosh/swt.jar
Binary file not shown.
27 changes: 25 additions & 2 deletions mac-support/edu/cmu/cs/hcii/cogtool/util/MacOSXPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.internal.cocoa.OS;

import java.lang.reflect.Method;
//import org.eclipse.wb.swt.SWTResourceManager;

// Note that this file will only compile against the Macintosh
Expand All @@ -117,11 +120,31 @@
public class MacOSXPlatform extends PlatformAdapter
{

public void setTheme(Display display) {
try {
// changing the appearance works only after the shell has been created
OS.setTheme(isSystemDarkAppearance());
// workaround for a bug in SWT: colors need to be reinited after changing the appearance
Method initColor = display.getClass().getDeclaredMethod("initColors");
initColor.setAccessible(true);
initColor.invoke(display);
}
catch (Exception e) {
e.printStackTrace();
}
}

@Override
public boolean isSystemDarkAppearance() {
return OS.isSystemDarkAppearance();
}

@Override
public void initPlatformMenu(Display display,
final PlatformMenuActions actions)
{
String appName = "CogTool";
{
setTheme(display);
String appName = "CogTool";

Menu systemMenu = Display.getDefault().getSystemMenu();

Expand Down

0 comments on commit 83a01fc

Please sign in to comment.