NSMenuBar mouse down notification? - cocoa

i'm trying to find notification/way to check if NSStatusItem from NSStatusBar (System wide bar) was clicked.
Does anybody knows how to achive this ?
Thanks
EDITED 20120111
I meant "any" item from whole status bar.

do something like:
statusItem.target = self;
statusItem.action = #selector(mouseDown:);
statusItem.sendActionOn = NSLeftMouseDownMask; // try without this at first - i can't remember what the default it so you probrably don't need it
then:
- (void)mouseDown:(id)sender {
NSLog(#"click click");
}

Related

SketchUp API: How to add a check box to a menu item

I don't see it anywhere in the Ruby API documentation but just in case I'm missing something...
I'm writing a plugin for SketchUp and I'm trying to add some options to the menu bar. One of my options would work best as a checkbox, but right now I have to have two separate buttons. Is there a way to create a checkbox menu item with the Ruby API?
Here's what I had to do instead:
foo = true
UI.menu("Plugins").add_item("Turn foo_option on") { #foo = true }
UI.menu("Plugins").add_item("Turn foo_option off") { #foo = false }
...and then I just use foo to change the options. Is there a cleaner way to do this?
SketchUp can have check marks in menu items. Both menu items and commands can have a validation proc. The documentation for set_validation_proc gives this example:
plugins_menu = UI.menu("Plugins")
item = plugins_menu.add_item("Test") { UI.messagebox "My Test Item"}
status = plugins_menu.set_validation_proc(item) {
if Sketchup.is_pro?
MF_ENABLED
else
MF_GRAYED
end
}
Although for checkmarks you would use the constants MF_CHECKED and MF_UNCHECKED
http://www.sketchup.com/intl/en/developer/docs/ourdoc/menu#set_validation_proc
http://www.sketchup.com/intl/en/developer/docs/ourdoc/command#set_validation_proc
I have not seen a checkbox menu item created from an extensions before, but I'm a beginning user so that's maybe why.
An other approach would be to do it like this:
unless file_loaded?(__FILE__)
plugin_menu = UI.menu("Plugin")
option_menu = plugin_menu.add_submenu("NameOfOption")
option_menu.add_item("OptionA"){ }
option_menu.add_item("OptionB"){ }
file_loaded(__FILE__)
end
The file_loaded?(_ FILE _) makes sure the menu only is created once, instead of every time you load your script. I hope this is helpfull. Maybe some experts now a way to create a checkbox menu.

Cocoa osx NSButton show text when mouse over

In my application for Mac I want to show some info text when the users moves the mouse pointer over a button.
Something like this:
How can I achieve this correctly?
Thanks in advance.
This works for me in Xcode 6.2:
In the Identity Inspector(the pane on the right hand side in the image below), in the Tool Tip section enter "Sad face":
In Interface-Builder you can set a 'tooltip' for most objects, including NSButton (Open the Inspector, then choose the "Help" section).
However, if you're using a NSToolbar, this also has tooltips; you may choose to do this programmatically. Try typing setToolTip in your source, then option-double-click it for more information. (option=alternate).
To programmatically add a custom tooltip in Swift, subclass the corresponding view
var trackingArea: NSTrackingArea!
Add a tracking area for the view
let opts: NSTrackingAreaOptions = ([NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways])
trackingArea = NSTrackingArea(rect: bounds, options: opts, owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
Mouse entered Event
override func mouseEntered(theEvent: NSEvent) {
self.tooltip = "Sad face : Select the option for very poor"
}
Or you can make a separate tooltip for each range of a string: https://stackoverflow.com/a/18814112/308315
You can also do it programmatically.
(Assume someButton is your NSButton object)
[someButton setToolTip:#"Sad face: Select this option for \"Very poor\""];

NSTextField with auto-suggestions like Safari's address bar?

What's the easiest way to have an NSTextField with a "recommendation list" dynamically shown below it as the user types? Just like Safari's address bar that has a menu of some sorts (I'm pretty confident Safari's address bar suggestions is menu since it has rounded corners, blue gradient selection, and background blurring).
I've tried using NSTextView's autocompletion facility but found it was inadequate:
It tries to complete words instead of the whole text fields – in other words, selecting an autocomplete suggestion will only replace the current word.
It nudges the autocompletion list forward and align it with the insertion point instead of keeping it align with the text field.
In the sample screenshot above whenever I selected the autocomplete suggestion the text field only replaces K with the suggested item in the list, which results in Abadi Abadi Kurniawan.
These are what I'd like to achieve:
Whenever a suggestion is selected, the entire text field is replaced with the suggestion.
Keep the suggestion list aligned with the text field's left side.
Note: This is not a question about adding progress indicator behind a text field.
The Safari address bar uses a separate window. Apple has example project CustomMenus and it only takes an hour or two to customize it.
Developer session explaining what has to be done Key Event Handling in Cocoa Applications
If you want to be able to select multiple words you need to provide own FieldEditor (credits should go for someone else)
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(nullable id)client;
{
if ([client isKindOfClass:[NSSearchField class]])
{
if (!_mlFieldEditor)
{
_mlFieldEditor = [[MLFieldEditor alloc] init];
[_mlFieldEditor setFieldEditor:YES];
}
return _mlFieldEditor;
}
return nil;
}
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
// suppress completion if user types a space
if (movement == NSRightTextMovement) return;
// show full replacements
if (charRange.location != 0) {
charRange.length += charRange.location;
charRange.location = 0;
}
[super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag];
if (movement == NSReturnTextMovement)
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"MLSearchFieldAutocompleted" object:self userInfo:nil];
}
}
This only addresses half of your answer, but I believe you need to subclass NSTextView and implement the - (NSRange)rangeForUserCompletion method, returning the range of the entire string in the text field. This should make sure that it doesn't just autocomplete the most recently entered word.
If you want a custom menu, you're going to have to do that yourself, probably by implementing the -controlTextDidChange: method and displaying a custom view with a table when appropriate.

how to know which Search Bar is Active?

now I have 2 search bar at one page
the first problem is How can I make a search button always show although no text at searchbar.text?
the second problem is, I have a Table View that Will show a different list expend which search bar I choose, how can I do it well?
I can set a variable that change everytime the search bar is active. However is there a way to see which search bar is currently the active search bar?
The simplest way to check which view are you working with, is assigning the tag property:
firstSearchBar.tag = 100;
secondSearchBar.tag = 200;
You can easily check it:
if(searhBar.tag == 100) {
// it is first search bar
} else if(searchBar.tag == 200) {
// it is second search bar
}
Now, the second part. If you want to show cancel button, you can do it in this way:
searchBar.showsCancelButton = YES;
If you want to show scope bar:
searchBar.showsScopeBar = YES;
If you want to show search results button:
searchBar.showsSearchResultsButton = YES;
EDIT: If you wish to show Search keyboard button even if there's no text entered, you can do it in this way:
UITextField *searchField = (UItextField *)[[searchBar subviews] objectAtIndex:0];
[searchField.enablesReturnKeyAutomatically = NO;
I recommend you reading UISearchBar's documentation.

Testing to see if a window is maximized

I noticed that in Windows, if you maximize a window you can not resize it until you un-maximized it again. This appears to be a normal behaviour, so I would like to remove my resize gripper when the window is maximised.
At the moment I can't find a property to detect if a window is maximized, and although I could add a boolean in my controller, it wouldn't necessarily catch requests to maximize from the OS.
So if you know of a reliable way to test if a window is maximized please let me know.
On a related note, I am using custom chrome, and when I maximize a window it overlaps the windows task bar. I can think of hacks to detect available screen size (using a transparent system chrome window), but it would be good to know of a better method.
Thanks
Rob
In your application (MXML) on the in the init method you ussually call on creationComplete:
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="init()" >
Add the following code:
this.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE, trackState);
the method looks like this:
public function trackState(event:NativeWindowDisplayStateEvent):void
{
if (event.afterDisplayState == NativeWindowDisplayState.MAXIMIZED)
{
isMaximised = true;
} else {
isMaximised = false;
}
}
I have figured out how this can best be done thanks to some pointers from TheBrain.
Firstly you need to watch for resize events to the window your want to control:
NativeApplication.nativeApplication.activeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, onWindowResize);
Then handle that event to decide if the window is maximised or not:
public function onWindowResize(event:NativeWindowBoundsEvent):void
{
if (event.afterBounds.height >= Screen.mainScreen.visibleBounds.height && event.afterBounds.width >= Screen.mainScreen.visibleBounds.width)
isMaximised = true;
else
isMaximised = false;
}
You then need to catch or create your own maximize button, and when clicked perform the following code:
if (isMaximised)
{
var bounds:Rectangle = Screen.mainScreen.visibleBounds;
NativeApplication.nativeApplication.activeWindow.bounds = bounds;
}
else
{
NativeApplication.nativeApplication.activeWindow.bounds = new Rectangle(100, 100, 500, 600);
}
You can modify the bounds to over maximize (which is handy for custom chrome windows with shadows), and you can also set the application to reset to a default size if the maximize button is clicked when it's already maximized (or do nothing).
I had issues about when to assign the window resize listner, and ended up removing and adding it every time the maximize button was clicked. It's a bit of overkill, but not too bad.
There is Win32 API Call that will do this for you:
BOOL IsZoomed( HWND hWnd );
to get the actual usable space from the screen, use the flash.display.Screen class, or you can use the systemMaxSize() which returns the largest window size allowed by the OS. For maximization you have some events that the window is dispaching when maximized/minimized/restored. You can find more info on the adobe pages (the link under systemMaxSize).
To detect if window is maximized...I don't think there is such a function (I might be wrong) but you can test if the app size is equal with the available screen size which means it's maximized. Or hook on the resize event which is triggered when the app is maximized/minimized/resized
Here is an easier way of checking if a window is maximized:
if(stage.nativeWindow.displayState == NativeWindowDisplayState.MAXIMIZED)
{
//do something
}
The following worked for me. No need to set event listeners, this code can be used to check the real-time state of the native window:
if (nativeWindow.displayState == 'maximized')
{
trace('Maximized');
}
else
{
trace('Minimized');
}
Can you use something like this to hook the maximize() event?

Resources