Image download from right click "Context Menu" not responding in the web page loaded in WKWebview. If any body knows help me to identify which delegate method will receive this call or is there any manual implementation needed to get this option work.
App:Mac Application. Language : Objective C. SDK: Xcode
You can intercept context menu items of the WKWebView class by subclassing it and implementing the willOpenMenu method like this:
class MyWebView: WKWebView {
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
for menuItem in menu.items {
if menuItem.identifier?.rawValue == "WKMenuItemIdentifierDownloadImage" ||
menuItem.identifier?.rawValue == "WKMenuItemIdentifierDownloadLinkedFile" {
menuItem.isHidden = true
}
}
}
}
Related
I want to show a dialog inside my tornadofx application, but I don't want to create the dialog in the view. I've tried to create a dialog at the controller, but this doesn't seem to work.
This is an working example on how I can create a dialog inside my View
class MainScreenSelect : View("tool") {
override val root = vbox {
dialog("dialog") {
// Code how the dialog looks and how it behaves
}
}
}
My problem is, that I don't want to create my dialog inside my View, I want to create the dialog inside my controller. I assign the Vbox of my View to a variable inside my controller and want to create the dialog inside my controller.
This will be my View then
class MainScreenSelect : View("tool") {
private val controller : Controller by inject()
override val root = vbox {
controller.vbox = this
controller.showDialog()
}
}
The Vbox of the View is assigned to a variable inside the controller and the next line should create a dialog.
My controller will look like this
class ChatScreenController : Controller() {
var vbox : Vbox by singleassign()
fun showDialog(){
vbox.apply{
dialog{} // Here is the error, I can't call dialog at this point, but I can
// call it if I do vbox.apply inside the View
}
}
My problem is, why can't I create the dialog inside my controller? I can create any other elements inside vbox.apply, like another vbox, button... , but no dialog. Where is my error and how can I create a dialog from a controller instead of a view?
edit: I already tried to create a dialog with
Dialog<R>().apply{
//CODE
}
This creates a dialog, but it doesn't lock my mainscreen to force an input and I can't close this window by pressing X(To be honest I didn't really know what I did with the Dialog, but if this is the way to go I'll look into it, how to work with this Dialog)
SOLVED: My mistake was, that I thought 'dialog' belonged to the UI Element vbox, but dialog is related to the View. Changing the variable inside the controller to View instead of an UI Element made it possible to create a dialog for this View.
This is the new Controller
class ChatScreenController : Controller() {
var view : View by singleassign()
fun showDialog(){
view.dialog{
// Code for the Dialog
}
}
The View needs to be changed to assign itself to the variable view inside the controller
class MainScreenSelect : View("tool") {
private val controller : Controller by inject()
override val root = vbox {
controller.view = this#MainScreenSelect
controller.showDialog()
}
}
I'm currently working on a project in Xamarin.iOS and I don't have any idea how the stack system works.
I have a menu where I choose the language of my app and I would like to refresh on the fly the language on all the previous pages when I select a different language.
When I tap on the previous button in my language settings the previous page is not translated so I decided to create a new ViewController that I put on the top of the stack with this.NavigationController.PushViewController(new ViewController(), true)
but I don't think it's the best way to do it so I tried
this.NavigationController.PopToRootViewController(true) to have the root ViewController but is there a way to get just the previous page on the stack?
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var cell = this.GetCell(tableView, indexPath);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
var previousIndexPath = NSIndexPath.FromRowSection(this.selectedIndex, 0);
this.selectedIndex = indexPath.Row;
var selectedLanguage = this.supportedCultures[this.selectedIndex];
Localization.Culture = selectedLanguage;
this.SaveLanguageToUserDefault(selectedLanguage);
this.TableView.ReloadRows(new NSIndexPath[] { previousIndexPath,indexPath }, UITableViewRowAnimation.None);
this.NavigationController.PopToRootViewController(true);
//this.NavigationController.PushViewController(new ViewController(), true);
}
In iOS if you are using a NavigationController and you want to Navigate to the previous ViewController you use the PopViewController Method in the NavigationProperty.
this.NavigationController.PopViewController(true);
This will make you app close the current page you are and will show the previous one.
Look that this is different to the PopToRootViewController.
Hope this helps.-
When you press back button ViewDidAppear medthod getting called in the coming page. Here you write your logic like after opening notification when going back setting count 0 etc
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
DashboardItem.NotificationCount = 0;
}
Hi... I am having an app in which I am using UIAlerController to show alert view.
I am sucessfully able to show it but the problem is I can not dismiss the view on touching outside. I have to create a view manually and a cancel button (shown in screenshot).
I know I can get the touch with putting Uialertactionstyle cancel but that makes my UI improper.
So Is there any other way so that I can get the touch event on outside touch?
Thanks in advance.
I try to use xcode tool to see the view hierarchy ,and it looks like as below:
I find the correct view to add tap gesture,so I modify the code and it works.
UIAlertController alert;
partial void UIButtonTouchUpInside(UIButton sender)
{
alert = UIAlertController.Create("title", "msg", UIAlertControllerStyle.ActionSheet);
this.PresentViewController(alert, true, () =>
{
UITapGestureRecognizer tap = new UITapGestureRecognizer(tapAction);
alert.View.Superview.Subviews[0].AddGestureRecognizer(tap);
});
}
void tapAction()
{
alert.DismissViewController(true, null);
}
I am making a Cocoa app that involves users selecting pictures from online for use within the app. I am also looking to collect contextual metadata from those downloads, such as the host the image came from, the website the user was visiting, the exact MIME type in the response headers, etc.
Basically I want to curate my user across the internet, downloading images and metadata into that user's account as I go. Until today I thought this would be impossible.
But just recently I was toying with a WKWebView and I tried right clicking on an image. I saw this...
Is there any way I could connect to that Download Image button, and get notifications when its clicked?
I did figure out how to make "Download Linked File" work and its a doozy that will not fit in a SO answer: https://github.com/kfix/MacPin/commit/9e5f925819f7f54ef29baff1e90783b820e683a3
However implementing those private delegate functions doesn't seem to allow "Download Image" to signal my WkWebView app in any way.
You can intercept those non-working "Download Image" and "Download Linked File" menu items by subclassing the WKWebView class and implementing the willOpenMenu method like this:
class MyWebView: WKWebView {
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
for menuItem in menu.items {
if menuItem.identifier == "WKMenuItemIdentifierDownloadImage" ||
menuItem.identifier == "WKMenuItemIdentifierDownloadLinkedFile" {
menuItem.action = #selector(menuClick(sender:))
menuItem.target = self
}
}
}
func menuClick(sender: AnyObject) {
if let menuItem = sender as? NSMenuItem {
Swift.print("Menu \(menuItem.title) clicked")
}
}
}
Instead of this you can also simply hide the menu items with menuItem.isHidden = true
Detecting the chosen menu item is one thing, but knowing what the user actually clicked in the WKWebView control is the next challenge :)
We have a parent Split view (NSSplitView), and two subviews, Content and SideBar (the sidebar is on the right).
What would be the optimal Cocoa-friendly way to toggle the SideBar view?
I would really love it, if the suggested solution includes animation
I really don't need any suggestions related to external plugins, etc (e.g. BWToolkit)
HINT : I've been trying to do that, but still I had issues hiding the divider of the NSSplitView as well. How could I do it, while hiding it at the same time?
Here's a pretty decent tutorial that shows how to do this: Unraveling the Mysteries of NSSplitView.
Hiding the divider is done in NSSplitView's delegate method splitView:shouldHideDividerAtIndex:.
You will have to animate the frame size change yourself if you don't like the way NSSplitView does it.
Easiest way to do it is as follows - and it's animated: [SWIFT 5]
splitViewItems[1].animator().isCollapsed = true // Show side pane
splitViewItems[1].animator().isCollapsed = false // hide side pane
I wrote a Swift version of the content in the link from #Nathan's answer that works for me. In the context of my example splitView is set elsewhere, probably as an instance property on an encompassing class:
func toggleSidebar () {
if splitView.isSubviewCollapsed(splitView.subviews[1] as NSView) {
openSidebar()
} else {
closeSidebar()
}
}
func closeSidebar () {
let mainView = splitView.subviews[0] as NSView
let sidepanel = splitView.subviews[1] as NSView
sidepanel.hidden = true
let viewFrame = splitView.frame
mainView.frame.size = NSMakeSize(viewFrame.size.width, viewFrame.size.height)
splitView.display()
}
func openSidebar () {
let sidepanel = splitView.subviews[1] as NSView
sidepanel.hidden = false
let viewFrame = splitView.frame
sidepanel.frame.size = NSMakeSize(viewFrame.size.width, 200)
splitView.display()
}
These functions will probably methods in a class, they are for me. If your splitView can be nil you obviously have to check for that. This also assumes you have two subviews and the one at index 1, here as sidePanel is the one you want to collapse.
In Xcode 9.0 with Storyboards open Application Scene select View->Menu->Show sidebar. CTRL-click Show Sidebar, in sent actions delete the provided one, click on x. From the circle CTRL drag to First Responder in application scene and select toggleSideBar to connect to. Open storyboard and select the first split view item and in attributes inspector change behaviour from default to sidebar. Run and try with view menu item show/hide. All done in interface builder no code. toggleSideBar handles the first split view item. https://github.com/Dis3buted/SplitViewController
I got some artifacts with the code above, likely because it was out of context. I am sure it works where it was meant to. Anyway, here is a very streamlined implementation:
// this is the declaration of a left vertical subview of
// 'splitViewController', which is the name of the split view's outlet
var leftView: NSView {
return self.splitViewController.subviews[0] as NSView
}
// here is the action of a button that toggles the left vertical subview
// the left subview is always restored to 100 pixels here
#IBAction func someButton(sender: AnyObject) {
if splitViewController.isSubviewCollapsed(leftView) {
splitViewController.setPosition(100, ofDividerAtIndex: 0)
leftView.hidden = false
} else {
splitViewController.setPosition(0, ofDividerAtIndex: 0)
leftView.hidden = true
}
}
To see a good example using animations, control-click to download this file.
If your NSSplitView control is part of a NSSplitViewController object, then you can simply use this:
splitViewController.toggleSidebar(nil)