Skip to content
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

Feature request: built-in automatic light and dark mode for the console #3725

Open
Oneechan69 opened this issue Dec 10, 2024 · 5 comments
Open

Comments

@Oneechan69
Copy link

Oneechan69 commented Dec 10, 2024

The Hammerspoon console is a native app window so like most native apps it should have an automatic light and dark theme. As shown in the comments below, this is a way of getting it right now without it built-in:

init.lua:

dm = require "darkmode"
dm.addHandler(
  function(dm2) 
    if dm2 then
      hs.console.darkMode(true)
    else
      hs.console.darkMode(false)
    end 
  end
)

if dm.getDarkMode() then
  hs.console.darkMode(true)
else
  hs.console.darkMode(false)     
end

darkmode.lua places in .hammerspoon (from here):

-- --------------------------------------------------------------------------
--
-- Implementation of a dark mode library for detecting and setting dark
-- mode in MacOS.
--
-- --------------------------------------------------------------------------
--
-- Example:
--
-- dm = require "darkmode"
-- dm.addHandler(function(dm2) print('darkmode changed:',dm2) end)
-- print('darkmode:',dm.getDarkMode())
-- dm.setDarkMode(not dm.getDarkMode())
-- print('darkmode:',dm.getDarkMode())
-- dm.setDarkMode(not dm.getDarkMode())
--
-- --------------------------------------------------------------------------

-- --------------------------------------------------------------------------
-- internal Data which should not be garbage collected
-- --------------------------------------------------------------------------

local internalData = {
	darkmode = false,
	watcher = nil,
	handler = {}
}

-- --------------------------------------------------------------------------
-- General functions
-- --------------------------------------------------------------------------

local function getDarkModeFromSystem()
	-- local _, darkmode = hs.osascript.applescript('tell application "System Events"\nreturn dark mode of appearance preferences\nend tell')
	local _, darkmode = hs.osascript.javascript("Application('System Events').appearancePreferences.darkMode.get()")
    return darkmode
end

local function getDarkMode()
	return internalData.darkmode
end

local function setDarkMode(state)
	hs.osascript.javascript(string.format("Application('System Events').appearancePreferences.darkMode.set(%s)",state))
end

local function addHandler(fn)
	-- add it here...
	internalData.handler[#internalData.handler+1] = fn
end

-- --------------------------------------------------------------------------
-- Internal functions
-- --------------------------------------------------------------------------

local function initialize()
	internalData.darkmode = getDarkModeFromSystem()
end

local function initializeWatcher()
	-- exit if already watching
	if internalData.watcher ~= nil then return end

	internalData.watcher = hs.distributednotifications.new(function(name, object, userInfo)
		local hasDarkMode = getDarkModeFromSystem()
		if hasDarkMode ~= internalData.darkmode then
			internalData.darkmode = hasDarkMode
			-- execute each handler with the darkmode as parameter (may change in future)
			for index, fn in ipairs(internalData.handler) do
				fn(hasDarkMode)
			end
		end
	end,'AppleInterfaceThemeChangedNotification')

	internalData.watcher:start()
end

-- --------------------------------------------------------------------------
-- Initialization
-- --------------------------------------------------------------------------

initialize()
initializeWatcher()

local module = {
	_ = internalData,
	setDarkMode = setDarkMode,
	getDarkMode = getDarkMode,
	addHandler = addHandler
}

return module

CleanShot 2024-12-17 at 05 27 23@2x
CleanShot 2024-12-17 at 05 27 13@2x

@samcarter
Copy link

Maybe try something like

dm = require "darkmode"
dm.addHandler(
  function(dm2) 
    if dm2 then
      hs.console.darkMode(true)
    else
      hs.console.darkMode(false)
    end 
  end
)

if dm.getDarkMode() then
  hs.console.darkMode(true)
else
  hs.console.darkMode(false)     
end

in your hammerspoon config

@Oneechan69
Copy link
Author

Where do I get the darkmode module?

@samcarter
Copy link

@Oneechan69 Oh, sorry, I forgot that this wasn't standard. You can get it from

#2386 (comment)

@Oneechan69
Copy link
Author

How do i use it?

@samcarter
Copy link

Place the file as darkmode.lua in the same folder as your init.lua

@Oneechan69 Oneechan69 changed the title Add a dark theme for the console? Feature request: built-in automatic light and dark mode for the console Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants