8

Large Type legacy celebrated in 80 line Go program

 3 years ago
source link: https://dev.to/progrium/large-type-legacy-celebrated-in-80-line-go-program-1mob
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Cover image for Large Type legacy celebrated in 80 line Go program

Large Type legacy celebrated in 80 line Go program

Jan 15

・2 min read

There is a little known feature in the MacOS Contacts app where if you right-click a phone number, you get a Large Type option. Large Type fills your screen with the phone number in the largest font size possible, allowing you or anybody else to easily see the number from far away. Nifty!

As far as I know, this feature has been in every version of OS X since its release back in 2000. Contacts was known as Address Book back then. The builtin calculator also had a Large Type view, but it seems it has since been removed. The feature might even exist in NeXTSTEP apps.

Presumably inspired by OS X Address Book, Large Type was also added to the legendary Quicksilver app launcher from the mid-2000's. Now, using Quicksilver, you could quickly type or paste in any text and display it in Large Type. More nifty!

Since I was involved in the SHDH hacker parties, this feature got a lot of use as a way to share a piece of information across a noisy, crowded room. Many users of Quicksilver also had a shell script that would run AppleScript to get Quicksilver to display text from the command line. Now it could be scripted!

It became the most memorable feature of my Quicksilver experience.

Large Type has the same sort of charm as the say command. It's nifty, maybe unnecessary, but really simple, there when you need it, and is kind of just a cool thing to play around with and show off.

The feature almost died with Quicksilver. Unsurprisingly, the modern Quicksilver equivalent, Alfred, also has Large Type. Except. I don't really want Alfred or Quicksilver installed just for Large Type now. It would be nice to have a standalone utility!

Thanks to a project I'm sharing in a couple weeks, I basically achieved perfection for such a utility. A standalone largetype command, in a single native code binary smaller than 4MB. And in under 80 lines of Go code:

package main

import ( "flag" "fmt" "runtime" "strings"

"github.com/progrium/macdriver/cocoa" "github.com/progrium/macdriver/core" "github.com/progrium/macdriver/objc" )

func main() { runtime.LockOSThread()

app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) { fontName := flag.String("font", "Helvetica", "font to use") flag.Parse()

screen := cocoa.NSScreen_Main().Frame().Size text := fmt.Sprintf(" %s ", strings.Join(flag.Args(), " ")) tr, fontSize := func() (rect core.NSRect, size float64) { t := cocoa.NSTextView_Init(core.Rect(0, 0, 0, 0)) t.SetString(text) for s := 70.0; s <= 550; s += 12 { t.SetFont(cocoa.Font(*fontName, s)) t.LayoutManager().EnsureLayoutForTextContainer(t.TextContainer()) rect = t.LayoutManager().UsedRectForTextContainer(t.TextContainer()) size = s if rect.Size.Width >= screen.Width*0.8 { break } } return rect, size }()

height := tr.Size.Height * 1.5 tr.Origin.Y = (height / 2) - (tr.Size.Height / 2) t := cocoa.NSTextView_Init(tr) t.SetString(text) t.SetFont(cocoa.Font(*fontName, fontSize)) t.SetEditable(false) t.SetImportsGraphics(false) t.SetDrawsBackground(false)

c := cocoa.NSView_Init(core.Rect(0, 0, 0, 0)) c.SetBackgroundColor(cocoa.Color(0, 0, 0, 0.75)) c.SetWantsLayer(true) c.Layer().SetCornerRadius(32.0) c.AddSubviewPositionedRelativeTo(t, cocoa.NSWindowAbove, nil)

tr.Size.Height = height tr.Origin.X = (screen.Width / 2) - (tr.Size.Width / 2) tr.Origin.Y = (screen.Height / 2) - (tr.Size.Height / 2)

w := cocoa.NSWindow_Init(core.Rect(0, 0, 0, 0), cocoa.NSBorderlessWindowMask, cocoa.NSBackingStoreBuffered, false) w.SetContentView(c) w.SetTitlebarAppearsTransparent(true) w.SetTitleVisibility(cocoa.NSWindowTitleHidden) w.SetOpaque(false) w.SetBackgroundColor(cocoa.NSColor_Clear()) w.SetLevel(cocoa.NSMainMenuWindowLevel + 2) w.SetFrameDisplay(tr, true) w.MakeKeyAndOrderFront(nil)

events := make(chan cocoa.NSEvent, 64) go func() { <-events cocoa.NSApp().Terminate() }() cocoa.NSEvent_GlobalMonitorMatchingMask(cocoa.NSEventMaskAny, events) }) app.ActivateIgnoringOtherApps(true) app.Run() }

Building

Note: For now, Apple Silicon users need to be set up for x86 mode

First, download Go or brew install go. Then, put largetype.go in a directory called largetype and from there run:

$ go mod init largetype
$ go build
Enter fullscreen modeExit fullscreen mode

This will make you a 4MB binary called largetype. Now you can run it:

$ ./largetype "Hello world"
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK