タブを管理しているView全体をTrakingAreaにして、mouseMovedでカーソルのあるタブを検出、「タブを閉じるボタン」を表示していなければ表示する、という流れになる。
CellにshowedCloseButtonというBOOL値を持たせて、YESなら描画処理の中で「閉じるボタン」を描画する。
if (showedCloseButton) {
[[self image] drawAtPoint:NSMakePoint(cellFrame.origin.x+CELL_LEFT_SPACING, (cellFrame.size.height-stringSize.height) /2) fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:0.8];
}
管理View側のmouseMoved、mouseExitedはこんな感じ。(汚いソースで恥ずかしいなあ、自分用備忘録なので勘弁して下さい)-(void)mouseMoved:(NSEvent *)theEvent{
NSPoint point = [self convertPointFromBase:[theEvent locationInWindow]];
NSInteger index = [self cellAtPoint:point];
if (index>-1) {
if (index==showedCloseButtonCellIndex) return;
else{
MyCell *cell;
if(showedCloseButtonCellIndex!=NO_SHOWED){
cell=[tabs objectAtIndex:showedCloseButtonCellIndex];
cell.showedCloseButton=NO;
}
cell=[tabs objectAtIndex:index];
if (!cell.showedCloseButton) {
cell.showedCloseButton=YES;
showedCloseButtonCellIndex=index;
[self setNeedsDisplay:YES];
}
}
}else{
if (showedCloseButtonCellIndex!=NO_SHOWED) {
MyCell* cell=[tabs objectAtIndex:showedCloseButtonCellIndex];
cell.showedCloseButton=NO;
[self setNeedsDisplay:YES];
showedCloseButtonCellIndex=NO_SHOWED;
}
}
}
-(void)mouseExited:(NSEvent *)theEvent{
if(showedCloseButtonCellIndex!=NO_SHOWED){
MyCell* cell=[tabs objectAtIndex:showedCloseButtonCellIndex];
cell.showedCloseButton=NO;
[self setNeedsDisplay:YES];
showedCloseButtonCellIndex=NO_SHOWED;
}
}
これで無事「閉じるボタン」が描画されるようになる。マウスがViewを抜けるとボタンも消える。
うーん、この後は「閉じるボタン」の上にカーソルが来た時の処理、マウスクリックされた時、mouseUpが来た時、と続くわけだが・・・。


0 件のコメント:
コメントを投稿