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

Add hs.styledtext support to hs.chooser #1624

Closed
latenitefilms opened this issue Dec 18, 2017 · 19 comments
Closed

Add hs.styledtext support to hs.chooser #1624

latenitefilms opened this issue Dec 18, 2017 · 19 comments

Comments

@latenitefilms
Copy link
Contributor

latenitefilms commented Dec 18, 2017

@cmsj - I was thinking about using hs.chooser as a font selection tool. Is it possible to add hs.styledtext support to hs.chooser?

To clarify, it would be great if the "text" and "subText" for each individual "choice" could be a hs.styledtext object - so that I could use hs.chooser as a font selection tool (i.e. each result will be displayed as a different font).

@cmsj
Copy link
Member

cmsj commented Dec 24, 2017

@latenitefilms do you mean to the search box, or the results?

@latenitefilms
Copy link
Contributor Author

Results would be amazing!

@latenitefilms latenitefilms mentioned this issue Dec 25, 2017
19 tasks
@latenitefilms
Copy link
Contributor Author

@cmsj & @asmagill - I'm happy to give this a bash myself if one of you can point me in the right direction?

@latenitefilms
Copy link
Contributor Author

Note to self:

NSAttributedString *theStr;
if (lua_type(L, 1) == LUA_TUSERDATA && luaL_testudata(L, 1, "hs.styledtext")) {
	theStr = [skin luaObjectAtIndex:1 toClass:"NSAttributedString"];
}

@cmsj
Copy link
Member

cmsj commented Apr 17, 2018

So, I think the branch I just pushed should address this, but I can't get it to actually do anything. I can see from the debugger that NSAttributedString objects are making it into the NSTableView delegate methods and being set on the choice cells, but my quick experiment with it doesn't work:

diff --git a/Spoons/Seal.spoon/seal_apps.lua b/Spoons/Seal.spoon/seal_apps.lua
index 5cb3111..cda6f77 100644
--- a/Spoons/Seal.spoon/seal_apps.lua
+++ b/Spoons/Seal.spoon/seal_apps.lua
@@ -79,7 +79,10 @@ function obj.choicesApps(query)
                 instances = hs.application.applicationsForBundleID(app["bundleID"])
             end
             if #instances > 0 then
-                choice["text"] = name .. " (Running)"
+--                choice["text"] = name .. " (Running)"
+                styled = hs.styledtext.new(name .. " (Running)")
+                styled:setStyle({color=hs.drawing.color.hammerspoon.osx_red}, -9, -1)
+                choice["text"] = styled
             else
                 choice["text"] = name
             end

I've never used hs.styledtext before - am I doing something wrong with it?

@cmsj
Copy link
Member

cmsj commented Apr 17, 2018

@latenitefilms maybe you could try my branch and let me know if you get any luck with it?

@latenitefilms
Copy link
Contributor Author

latenitefilms commented Apr 17, 2018

@cmsj - Soooooooo close! The subText works, but the text doesn't.

Maybe this line is the issue?

Here's my testing code:

local chooser = require("hs.chooser")
local console = require("hs.console")
local inspect = require("hs.inspect")
local styledtext = require("hs.styledtext")

local completionFn = function(result)
	print(string.format("Result: %s", result and inspect(result)))
end

local fancyText = styledtext.new("First Choice", {
        color = {hex = "#FF0000", alpha = 1},
        font = { name = "Futura", size = 20 },
    })

local fancySubText = styledtext.new("This is the subtext of the first choice", {
        color = {hex = "#FF0000", alpha = 1},
        font = { name = "Futura", size = 20 },
    })

console.printStyledtext(fancyText)
console.printStyledtext(fancySubText)

local choices = {
	{
		["text"] = fancyText,
		["subText"] = fancySubText,
		["uuid"] = "0001"
	},
	{ 
		["text"] = "Second Option",
		["subText"] = "I wonder what I should type here?",
		["uuid"] = "Bbbb"
	},
	{ 
		["text"] = "Third Possibility",
		["subText"] = "What a lot of choosing there is going on here!",
		["uuid"] = "III3"
	},
}

theChooser = chooser.new(completionFn)
	:choices(choices)
	:show()

@latenitefilms
Copy link
Contributor Author

Ummm... I'm not sure what's going on. The code you've added definitely seems to get setting text to
an NSAttributedString. Something must be happening to it AFTER - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row?

screen shot 2018-04-18 at 8 36 10 am

@cmsj
Copy link
Member

cmsj commented Apr 17, 2018

@latenitefilms try now?

@latenitefilms
Copy link
Contributor Author

No luck I'm afraid. subText works fine, text does not, which is weird. hs.styledtext is being passed back in the completionFn.

Here's my test code:

local chooser = require("hs.chooser")
local console = require("hs.console")
local inspect = require("hs.inspect")
local styledtext = require("hs.styledtext")

console.clearConsole()

local completionFn = function(result)
	print(string.format("Result: %s", result and inspect(result)))	
	
	print("text:")
	console.printStyledtext(result.text)
	print("subText:")
	console.printStyledtext(result.subText)

end

local text = styledtext.new("First Choice", {
        color = {hex = "#FF0000", alpha = 1},
        font = { name = "Futura", size = 20 },
    })

local subText = styledtext.new("This is the subtext of the first choice", {
        color = {hex = "#FF0000", alpha = 1},
        font = { name = "Futura", size = 20 },
    })

print("text:")
console.printStyledtext(text)
print("subText:")
console.printStyledtext(subText)

local choices = {
	{
		["text"] = text,
		["subText"] = subText,
		["uuid"] = "1"
	},
	{ 
		["text"] = text,
		["subText"] = subText,
		["uuid"] = "2"
	},
	{ 
		["text"] = text,
		["subText"] = subText,
		["uuid"] = "3"
	},
}

theChooser = chooser.new(completionFn)
	:choices(choices)
	:show()

This is what it looks like:

screen shot 2018-04-18 at 9 12 11 am

Console looks like this:

screen shot 2018-04-18 at 9 12 47 am

I'll keep trying to work out why subText works fine, but text doesn't. Very odd.

Thanks for all your help!!

@latenitefilms
Copy link
Contributor Author

Ok, worked it out!

No idea what this means or how it works, but if you opened up HSChooserWindow.xib the Text Chooser Cell had two "Referencing Outlets", whereas the Sub Text only had one, so I removed the second one to match, and problem solved:

screen shot 2018-04-18 at 9 19 22 am

@latenitefilms
Copy link
Contributor Author

@cmsj - Note, you need to remove the second value from both the Text row without the subtext and the one below it without.

@latenitefilms
Copy link
Contributor Author

This test code:

local chooser = require("hs.chooser")
local console = require("hs.console")
local inspect = require("hs.inspect")
local styledtext = require("hs.styledtext")

console.clearConsole()

local completionFn = function(result)
	print(string.format("Result: %s", result and inspect(result)))	
	
	print("text:")
	console.printStyledtext(result.text)
	print("subText:")
	console.printStyledtext(result.subText)

end

local text = styledtext.new("Styled Text", {
        color = {hex = "#FF0000", alpha = 1},
        font = { name = "Futura", size = 18 },
    })

local subText = styledtext.new("Styled Subtext", {
        color = {hex = "#800000", alpha = 1},
        font = { name = "Futura", size = 10 },
    })

print("text:")
console.printStyledtext(text)
print("subText:")
console.printStyledtext(subText)

local choices = {
	{
		["text"] = text,
		["subText"] = subText,
		["uuid"] = "1"
	},
	{ 
		["text"] = "Plain Text",
		["subText"] = "Plain Text Subchoice",
		["uuid"] = "2"
	},
	{ 
		["text"] = text,
		["uuid"] = "3"
	},
	{ 
		["text"] = "Plain Text",
		["uuid"] = "3"
	},		
}

theChooser = chooser.new(completionFn)
	:choices(choices)
	:show()

...gives me:

screen shot 2018-04-18 at 9 37 07 am

cmsj added a commit to cmsj/hammerspoon that referenced this issue Apr 18, 2018
@cmsj
Copy link
Member

cmsj commented Apr 18, 2018

@latenitefilms good catch! thanks :)

@latenitefilms
Copy link
Contributor Author

@cmsj - Amazing! THANK YOU! Is it worth adding a note that hs.chooser supports hs.styledtext in the in-line documentation?

@cmsj
Copy link
Member

cmsj commented Apr 18, 2018

Good point :)

@latenitefilms
Copy link
Contributor Author

@cmsj - Not sure what's changed, but this now seems to be broken in 0.9.66?

Using the same test code above, now gives me:

screen shot 2018-05-08 at 9 11 47 am

I think maybe something has gone funky in LuaSkin - given we're also seeing this:

#1441 (comment)

Thoughts?

@latenitefilms
Copy link
Contributor Author

Doh! Sorry - ignore me, I was testing in 0.9.66, rather than the latest master branch.

@latenitefilms
Copy link
Contributor Author

@cmsj - Ok, I've worked out what's broken - but not sure how to fix.

This works:

local chooser = require("hs.chooser")
local console = require("hs.console")
local inspect = require("hs.inspect")
local styledtext = require("hs.styledtext")

console.clearConsole()

local completionFn = function(result)
	print(string.format("Result: %s", result and inspect(result)))	
	
	print("text:")
	console.printStyledtext(result.text)
	print("subText:")
	console.printStyledtext(result.subText)

end

local text = styledtext.new("Styled Text", {
        color = {hex = "#FF0000", alpha = 1},
        font = { name = "Futura", size = 18 },
    })

local subText = styledtext.new("Styled Subtext", {
        color = {hex = "#800000", alpha = 1},
        font = { name = "Futura", size = 10 },
    })

print("text:")
console.printStyledtext(text)
print("subText:")
console.printStyledtext(subText)

local choices = {}

for i, v in pairs(styledtext.fontNames()) do
	local choice = {}	
	if string.sub(v, 1, 1) ~= "." then 
		local displayName = styledtext.fontInfo(v).displayName
		choice.text = styledtext.new(displayName, {
			font = { name = v, size = 18 },
		})
		choice.subText = ""
		choice.uuid = choice.text
		table.insert(choices, choice)
	end		
end

theChooser = chooser.new(completionFn)
	:choices(choices)
	:show()

...but if you remove choice.subText = "", then it only shows plain text?

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants