- fix the issue of previous selected on-screen widget get deleted when deleting legacy osc button

- coding improvements for on-screen widget deletion
This commit is contained in:
TrueZhuangjia
2025-03-02 21:47:57 +08:00
parent ebec9877b0
commit 70e5b3520c
6 changed files with 39 additions and 21 deletions
@@ -190,6 +190,8 @@
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Reset variables related to on screen controller button drag and drop routine. These variables should be reset in 'touchesCancelled' or 'touchesEnded' but these may not be called in unaccounted-for edge cases such as when the user opens various OS-level control center related views by dragging down from the top of the screen, or dragging up from the bottom of the screen. Since Apple likes to add new control centers and new ways of opening them (i.e. Dynamic Island on iPhone 14 Pro) it's best to reset these variables here when the user is beginning a new on screen controller button drag routine */
[[NSNotificationCenter defaultCenter] postNotificationName:@"LegacyOscCALayerSelectedNotification" object:self];
layerBeingDragged = nil;
horizontalGuideline.backgroundColor = [UIColor blueColor];
verticalGuideline.backgroundColor = [UIColor blueColor];
@@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (OSCProfilesManager *) sharedManager;
+ (void) setOnScreenWidgetViewsDict:(NSMutableDictionary* )dict;
+ (void) setOnScreenWidgetViewsSet:(NSMutableSet* )set;
#pragma mark - Getters
/**
+14 -6
View File
@@ -14,7 +14,7 @@
@implementation OSCProfilesManager
static NSMutableDictionary *OnScreenWidgetViewsDict;
static NSMutableSet *OnScreenWidgetViews;
#pragma mark - Initializer
@@ -32,8 +32,8 @@ static NSMutableDictionary *OnScreenWidgetViewsDict;
#pragma mark - Class Helper Methods
+ (void) setOnScreenWidgetViewsDict:(NSMutableDictionary* )dict{
OnScreenWidgetViewsDict = dict;
+ (void) setOnScreenWidgetViewsSet:(NSMutableSet* )set{
OnScreenWidgetViews = set;
}
/**
@@ -240,6 +240,14 @@ static NSMutableDictionary *OnScreenWidgetViewsDict;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded requiringSecureCoding:YES error:nil];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"];
[[NSUserDefaults standardUserDefaults] synchronize];
// saving test:
/*
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *path = [documentsPath stringByAppendingPathComponent:@"newDefault.bin"];
[newProfileEncoded writeToFile:path atomically:YES];
NSLog(@"默认 Profile 数据已保存到: %@", path);
*/
}
}
@@ -282,8 +290,7 @@ static NSMutableDictionary *OnScreenWidgetViewsDict;
}
// save on-screen widget views (keyboard & mouse command) as buttonstate:
[OnScreenWidgetViewsDict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { // this dict is passed from the layout tool VC, as a static obj in this class.
OnScreenWidgetView *widgetView = value;
for(OnScreenWidgetView* widgetView in OnScreenWidgetViews){
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:widgetView.keyString buttonType:CustomOnScreenWidget andPosition:widgetView.center];
buttonState.alias = widgetView.keyLabel;
buttonState.timestamp = widgetView.timestamp;
@@ -296,7 +303,8 @@ static NSMutableDictionary *OnScreenWidgetViewsDict;
NSData *buttonStateEncoded = [NSKeyedArchiver archivedDataWithRootObject:buttonState requiringSecureCoding:YES error:nil];
[buttonStatesEncoded addObject: buttonStateEncoded];
}];
}
return buttonStatesEncoded;
}
-2
View File
@@ -20,7 +20,6 @@ import UIKit
}
@objc static public var editMode: Bool = false
@objc static public var timestampOfButtonBeingDragged: TimeInterval = 0
@objc public var keyLabel: String
@objc public var keyString: String
@objc public var timestamp: TimeInterval
@@ -937,7 +936,6 @@ import UIKit
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
OnScreenWidgetView.timestampOfButtonBeingDragged = self.timestamp
if !OnScreenWidgetView.editMode {
if CommandManager.touchPadCmds.contains(self.keyString){
@@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reloadOnScreenWidgetViews;
@property LayoutOnScreenControls *layoutOSC; // object that contains a view which contains the on screen controller buttons that allows the user to drag and positions each button on the screen using touch
@property (nonatomic) NSMutableDictionary* OnScreenWidgetViewsDict;
@property (nonatomic) NSMutableSet* OnScreenWidgetViews;
@property int OSCSegmentSelected;
@@ -60,7 +60,7 @@
- (void) viewWillDisappear:(BOOL)animated{
OnScreenWidgetView.editMode = false;
for (OnScreenWidgetView* widgetView in self.OnScreenWidgetViewsDict.allValues){
for (OnScreenWidgetView* widgetView in self.OnScreenWidgetViews){
[widgetView.stickBallLayer removeFromSuperlayer];
[widgetView.crossMarkLayer removeFromSuperlayer];
}
@@ -77,7 +77,7 @@
}
}
[self.OnScreenWidgetViewsDict removeAllObjects];
[self.OnScreenWidgetViews removeAllObjects];
NSLog(@"reload os Key here");
@@ -101,7 +101,7 @@
[widgetView resizeWidgetView]; // resize must be called after relocation
[widgetView adjustTransparencyWithAlpha:buttonState.backgroundAlpha];
[widgetView adjustBorderWithWidth:buttonState.borderWidth];
[self.OnScreenWidgetViewsDict setObject:widgetView forKey:@(widgetView.timestamp)];
[self.OnScreenWidgetViews addObject:widgetView];
}
}
}
@@ -110,8 +110,8 @@
- (void) viewDidLoad {
[super viewDidLoad];
profilesManager = [OSCProfilesManager sharedManager];
self.OnScreenWidgetViewsDict = [[NSMutableDictionary alloc] init]; // will be revised to read persisted data , somewhere else
[OSCProfilesManager setOnScreenWidgetViewsDict:self.OnScreenWidgetViewsDict]; // pass the keyboard button dict to profiles manager
self.OnScreenWidgetViews = [[NSMutableSet alloc] init]; // will be revised to read persisted data , somewhere else
[OSCProfilesManager setOnScreenWidgetViewsSet:self.OnScreenWidgetViews]; // pass the keyboard button dict to profiles manager
isToolbarHidden = NO; // keeps track if the toolbar is hidden up above the screen so that we know whether to hide or show it when the user taps the toolbar's hide/show button
@@ -154,6 +154,11 @@
[profilesManager saveProfileWithName:@"Default" andButtonLayers:self.layoutOSC.OSCButtonLayers];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearSelectedWidgetView)
name:@"LegacyOscCALayerSelectedNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(profileRefresh)
name:@"OscLayoutTableViewCloseNotification"
@@ -375,7 +380,7 @@
OnScreenWidgetView* widgetView = [[OnScreenWidgetView alloc] initWithKeyString:cmdString keyLabel:keyLabel];
widgetView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
widgetView.timestamp = CACurrentMediaTime(); // will be set as key in in the dict.
[self.OnScreenWidgetViewsDict setObject:widgetView forKey:@(widgetView.timestamp)];
[self.OnScreenWidgetViews addObject:widgetView];
// Add the widgetView to the view controller's view
[self.view addSubview:widgetView];
[widgetView setLocationWithXOffset:50 yOffset:50];
@@ -406,6 +411,10 @@
}
}
- (void)clearSelectedWidgetView{
selectedWidgetView = nil;
}
- (void)widgetViewTapped: (NSNotification *)notification{
// receive the selected widgetView obj passed from the notification
OnScreenWidgetView* widgetView = (OnScreenWidgetView* )notification.object;
@@ -820,11 +829,12 @@
// removing keyboard buttons objs
UITouch *touch = [touches anyObject]; // Get the first touch in the set
if([self touchWithinTashcanButton:touch]){
[self.OnScreenWidgetViewsDict[@(OnScreenWidgetView.timestampOfButtonBeingDragged)] removeFromSuperview];
[self.OnScreenWidgetViewsDict removeObjectForKey:@(OnScreenWidgetView.timestampOfButtonBeingDragged)];
[selectedWidgetView.stickBallLayer removeFromSuperlayer];
[selectedWidgetView.crossMarkLayer removeFromSuperlayer];
OnScreenWidgetView.timestampOfButtonBeingDragged = 0; //reset thie timestamp
if(self->selectedWidgetView != nil){
[self->selectedWidgetView removeFromSuperview];
[self.OnScreenWidgetViews removeObject:self->selectedWidgetView];
[selectedWidgetView.stickBallLayer removeFromSuperlayer];
[selectedWidgetView.crossMarkLayer removeFromSuperlayer];
}
}