objective c - How to differentiate the mouseDown event from mouseDragged in a Transparent NSWindow -
have transparent nswindow simple icon in can dragged around screen.
code is:
.h:
@interface customview : nswindow{ } @property (assign) nspoint initiallocation; .m
@synthesize initiallocation; - (id) initwithcontentrect: (nsrect) contentrect stylemask: (nsuinteger) astyle backing: (nsbackingstoretype) bufferingtype defer: (bool) flag{ if (![super initwithcontentrect: contentrect stylemask: nsborderlesswindowmask backing: bufferingtype defer: flag]) return nil; [self setbackgroundcolor: [nscolor clearcolor]]; [self setopaque:no]; [nsapp activateignoringotherapps:yes]; return self; } - (void)mousedragged:(nsevent *)theevent { nsrect screenvisibleframe = [[nsscreen mainscreen] visibleframe]; nsrect windowframe = [self frame]; nspoint neworigin = windowframe.origin; // mouse location in window coordinates. nspoint currentlocation = [theevent locationinwindow]; // update origin difference between new mouse location , old mouse location. neworigin.x += (currentlocation.x - initiallocation.x); neworigin.y += (currentlocation.y - initiallocation.y); // don't let window dragged under menu bar if ((neworigin.y + windowframe.size.height) > (screenvisibleframe.origin.y + screenvisibleframe.size.height)) { neworigin.y = screenvisibleframe.origin.y + (screenvisibleframe.size.height - windowframe.size.height); } // move window new location [self setframeorigin:neworigin]; } - (void)mousedown:(nsevent *)theevent { // mouse location in window coordinates. self.initiallocation = [theevent locationinwindow]; } i want display nspopover when users clicks image of transparent window. but, can see in code, mousedown event used mouse location (the code above taken example).
can know when user clicks icon drag around or clicked display nspopover?
thank you
this classic situation of receiving defining event after need in order begin action. specifically, can't know if mousedown beginning of drag until after drag starts. however, want act upon mousedown if drag doesn't start.
in ios (i realize that's not directly relevant code here, instructional), there's entire api built around letting ios attempt make these kinds of decisions you. entire gesture system based on idea user starts might 1 of many different actions, , needs resolved on time, possibly resulting in cancelled actions during tracking period.
on os x, don't have many systems out this, if have needs handle click , drag differentially, need defer next action until guard time has passed, , if passes, can perform original action. in case, want following:
in mousedown, begin nstimer set appropriate guard time (not long people accidentally move pointer, , not short you'll trigger before user drags) in order call later trigger popover.
in mousedragged, use guard area make sure if user twitches little, doesn't count drag. can irritating, results in needing drag farther seems necessary in order begin drag, you'll want either find magic constant somewhere, or experimentation. when guard area exceeded, begin legitimate drag operation canceling nstimer [timer invalidate] , drag.
in callback timer, display popover. if user dragged, nstimer have been invalidated, causing not fire, , popover won't displayed.
Comments
Post a Comment