Skip to content

Add support for dynamic colors #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,29 @@ Did the terminal window expand to cover the area previously occupied by Safari?
![stackline setup 01](assets/stackline-setup-01@2x.png)

The default stack indicator style is a "pill" as seen ↑
To toggle icons:

* To toggle icons:

```sh
echo ":toggle_icons:1" | hs -m stackline-config
# Turn on icons
echo ":show_icons:1" | hs -m stackline-config
# Turn off icons
echo ":show_icons:0" | hs -m stackline-config
```

![stackline setup 02](assets/stackline-icon-indicators.png)

* To toggle colors:

```sh
# Turn on colors
echo ":indicator_colors:1" | hs -m stackline-config
# Turn off colors
echo ":indicator_colors:0" | hs -m stackline-config
```

![stackline setup 03](assets/stackline-color-indicators.png)

Image (and feature!) courtesy of [@alin23](https://github.com/alin23).

## Help us get to v1.0.0!
Expand Down
Binary file added assets/stackline-color-indicators.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-github-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-github-banner@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-icon-indicators.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-logo@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/stackline-setup-01@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 17 additions & 16 deletions stackline/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ require("hs.ipc")

local Stack = require 'stackline.stackline.stack'
local tut = require 'stackline.utils.table-utils'
local utils = require 'stackline.utils.utils'

print(hs.settings.bundleID)

function getOrSet(key, val)
local existingVal = hs.settings.get(key)
if existingVal == nil then
hs.settings.set(key, val)
return val
end
return existingVal
end

local showIcons = getOrSet("showIcons", false)
local showIcons = utils.settingsGetOrSet("show_icons", false)
wsi = Stack:newStackManager(showIcons)

local shouldRestack = tut.Set{
Expand All @@ -24,7 +16,8 @@ local shouldRestack = tut.Set{
"window_destroyed",
"window_resized",
"window_moved",
"toggle_icons",
"show_icons",
"indicator_colors",
}

local shouldClean = tut.Set{
Expand All @@ -34,21 +27,29 @@ local shouldClean = tut.Set{
"window_minimized",
}

function resetLuminanceCache(paths, flagTables)
print("Changed wallpaper, resetting luminanceCache")
utils.luminanceCache = {}
wsi.cleanup()
wsi = Stack:newStackManager(hs.settings.get("show_icons"))
end

local wallpaperDB = os.getenv("HOME") .. "/Library/Application Support/Dock/"
print("Watching", wallpaperDB)
wallpaperWatcher = hs.pathwatcher.new(wallpaperDB, resetLuminanceCache):start()

function configHandler(_, msgID, msg)
if msgID == 900 then
return "version:2.0a"
end

if msgID == 500 then
key, value = msg:match(".+:([%a_-]+):([%a%d_-]+)")
if key == "toggle_icons" then
showIcons = not showIcons
hs.settings.set("showIcons", showIcons)
end
hs.settings.set(key, utils.boolean(string.lower(value)))

if shouldRestack[key] then
wsi.cleanup()
wsi = Stack:newStackManager(showIcons)
wsi = Stack:newStackManager(hs.settings.get("show_icons"))
end
wsi.update(shouldClean[key])
end
Expand Down
20 changes: 16 additions & 4 deletions stackline/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ end -- }}}

function Window:process(showIcons, currTabIdx) -- {{{
-- Config
local indicatorColors = _.settingsGetOrSet("indicator_colors", false)

self.showIcons = showIcons
local unfocused_color = {white = 0.9, alpha = 0.30}
local focused_color = {white = 0.9, alpha = 0.99}
local unfocused_color_light = {white = 0.9, alpha = 0.40}
local focused_color_light = indicatorColors and
{hue = 0.1, saturation = 1.0, brightness = 0.9, alpha = 0.95} or
{white = 0.9, alpha = 0.99}
local unfocused_color_dark = {white = 0.1, alpha = 0.40}
local focused_color_dark = indicatorColors and
{hue = 0.99, saturation = 0.8, brightness = 0.8, alpha = 0.95} or
{white = 0.1, alpha = 0.99}
local padding = 4
local iconPadding = 4
local aspectRatio = 5
Expand All @@ -96,6 +104,7 @@ function Window:process(showIcons, currTabIdx) -- {{{
local width = self.showIcons and size or (size / aspectRatio)

local shit = self.frame.x - (width + padding)

self.canvas_frame = {
x = shit,
y = self.frame.y + 2,
Expand All @@ -105,7 +114,7 @@ function Window:process(showIcons, currTabIdx) -- {{{

self.indicator_rect = {
x = 0,
y = ((currTabIdx - 1) * size * 1.1),
y = _.round((currTabIdx - 1) * size * 1.1),
w = width,
h = size,
}
Expand All @@ -117,8 +126,11 @@ function Window:process(showIcons, currTabIdx) -- {{{
h = self.indicator_rect.h - (iconPadding * 2),
}

light_bg = _.hasLightBG(self.canvas_frame, self.indicator_rect)
self.color_opts = {
bg = self.focused and focused_color or unfocused_color,
bg = self.focused and
(light_bg and focused_color_dark or focused_color_light) or
(light_bg and unfocused_color_dark or unfocused_color_light),
canvasAlpha = self.focused and 1 or 0.2,
imageAlpha = self.focused and 1 or 0.4,
}
Expand Down
71 changes: 70 additions & 1 deletion utils/utils.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,78 @@
local tut = require 'stackline.utils.table-utils'
local LIGHT_BG_BRIGHTNESS_THRESHOLD = 0.5

local trueValues = tut.Set{
"1",
"t",
"y",
"true",
"yes",
"on",
}

utils = {}

utils.map = hs.fnutils.map
utils.concat = hs.fnutils.concat
utils.each = hs.fnutils.each


utils.luminanceCache = {}

utils.luminance = function(color)
color = hs.drawing.color.asHSB(color)
return (2 - color.saturation) * color.brightness / 2
end

utils.pixelBrightness = function(image, pixel)
color = image:colorAt(pixel)
return color and utils.luminance(color) or 0.5
end

utils.hasLightBG = function(canvasFrame, indicatorFrame)
screen = hs.screen.mainScreen()

frame = screen:absoluteToLocal(hs.geometry(canvasFrame))
frame = hs.geometry(indicatorFrame.x + frame.x, indicatorFrame.y + frame.y, indicatorFrame.w, indicatorFrame.h)

if utils.luminanceCache[frame.string] ~= nil then
return utils.luminanceCache[frame.string]
end

image = screen:snapshot(frame)

topleft = frame.topleft
brightness = 0.0
for dx=0,frame.w do
for dy=0,frame.h do
brightness = brightness + utils.pixelBrightness(image, hs.geometry.point(dx, dy))
end
end

brightness = brightness / (frame.w * frame.h)
print(brightness, frame)
utils.luminanceCache[frame.string] = brightness > LIGHT_BG_BRIGHTNESS_THRESHOLD
return utils.luminanceCache[frame.string]
end

utils.round = function(x)
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end

utils.boolean = function(val)
return trueValues[val]
end

utils.settingsGetOrSet = function(key, val)
local existingVal = hs.settings.get(key)
if existingVal == nil then
hs.settings.set(key, val)
return val
end
return existingVal
end


utils.filter = function(t, f)
local out = {}
for k, v in pairs(t) do
Expand Down Expand Up @@ -272,7 +341,7 @@ function utils.p(...)
-- p('# to be cleaned: ', table.length(self.tabStacks))
-- -> "# to be cleaned: 2"
-- Example 2:
-- p('keys be cleaned: ', table.keys(self.tabStacks))
-- p('keys be cleaned: ', table.keys(self.tabStacks))
-- -> "keys be cleaned: { "6207671375", "63771631066207041183" }"

result = {}
Expand Down