Repository navigation
Track MyApplication property inside app designer rather than through project system - #4714
Conversation
| 'We only want to save the project file itself, not any other its children (any of the other files in the project) | ||
| hr = rdt.SaveDocuments(CUInt(__VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveIfDirty Or __VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveNoChildren), | ||
| 'but we do want to force it so that the MyApplication file will be saved is necessary | ||
| hr = rdt.SaveDocuments(CUInt(__VSRDTSAVEOPTIONS.RDTSAVEOPT_ForceSave Or __VSRDTSAVEOPTIONS.RDTSAVEOPT_SaveNoChildren), |
There was a problem hiding this comment.
This actually fixes a bug in the current system, when changing something on a property page that changes the .myapp file and not the project file, we mark the properties tab as dirty but when save is pressed the SaveIfDirty flag means it doesn't actually save, so the .myapp file doesn't save either. If the user then closes VS they get asked to save it, but from experience I can tell you that when VS asks you to save a file that you didn't manually change, you get used to clicking "Don't Save", and in this case you would lose changes. Forcing a save here means at least the running document table events will fire.
| Public Overrides Function WriteUserDefinedProperty(PropertyName As String, Value As Object) As Boolean | ||
| If PropertyName = Const_EnableVisualStyles Then | ||
| If PropertyName = Const_MyApplication Then | ||
| Dim x = MyApplicationProperties |
There was a problem hiding this comment.
This is probably unnecessary, but its a lazy loading property so just in case? ¯\_(ツ)_/¯
| Managed; | ||
| ClassDesigner;" /> | ||
| ClassDesigner; | ||
| PreventAutomaticMyApplication;" /> |
There was a problem hiding this comment.
This is what opts us out of the new system, so we don't break everyones existing SDK-style VB projects by showing them property page controls that may not work 100%
| Dim cookie = GetProjectFileCookie(projectHierarchy) | ||
|
|
||
| ' Because GetOrAdd always creates a new value, even if using a factory, we just create an object and only | ||
| ' initialize it later, relying on the face that MyApplicationProperties will not initialize itself twice |
| Private Function TrackInternal(projectHierarchy As IVsHierarchy) As MyApplicationProperties | ||
| Dim cookie = GetProjectFileCookie(projectHierarchy) | ||
|
|
||
| ' Because GetOrAdd always creates a new value, even if using a factory, we just create an object and only |
There was a problem hiding this comment.
Can you clarify what you mean here? I believe the func-accepting overload avoids constructing NewApplicationProperties when the key exists in the collection. It can still create redundant objects in the unlikely case where the optimistic algorithm fails to commit, so cannot be used in cases where you need to guarantee the value is only created once. But in this case I think it's fine.
There was a problem hiding this comment.
I could have sworn I looked at this, and saw that valueFactory was always called, but looking now you're right it isn't called if the key exists. I guess I could change this to use a Func then, but I'd still want to keep the Init call out of it just in case, as that does a bunch of interop stuff and calls into the hierarchy that its worth avoiding IMO. I could also change this to use a Lazy.. too many options 😛
There was a problem hiding this comment.
Changed this to a lambda, and corrected the comment.
Fixes #4708, part of #4249.
The Situation:
VB projects have an application framework they can opt in to which is enabled by the property pages through the
MyApplicationproperty of theBrowseObject. This object has 8 properties and stores its data in a.myappfile under theMy Projectfolder. This file is then used to drive a generator that generates source code that is included in the users project.Currently the legacy implementation of MyApplication simply creates an instance of our MyApplicationManager, initializes it, and returns the result of calling
GetProperties(). It then callsSavewhen the project file saves, andClosewhen the project closes.If the MyApplication property returns a value (ie, the system is in place) it also enables the bottom half of the VB win forms property page which contains a bunch of extra controls most of which work.
The Problem:
CPS currently doesn't have any way (that I can see) to have a property on the BrowseObject that is not reduced to a
stringat some point.The Solution:
Since we essentially control everything around MyApplication anyway, my solution is to take full control over its lifetime without worrying about the project system in use at all. This approach has been discussed with David Kean and unless I misunderstood something, is what we discussed. Essentially we have a class that tracks the lifetime of projects, and supplies the appropriate properties object for each when requested, saves when the project is saved, and closes when the project closes. The trackers lifetime is global (its a static/Shared) but it's only initialized when first used. It's used by both legacy and CPS project systems so is independent of both.
At the moment this has been done in a way that only works in legacy (we opt out with a capability) but once the rest of the features of the property pages work we would remove this capability and this would just work by itself as a feature of the property pages. The legacy implementation of the MyApplication property could also be removed since it no longer works with this PR.
The Issues:
It is a fair assumption to make that we're the only consumers of these property pages (managed and legacy), and certainly from a search on GitHub and //index it seems that nobody calls the MyApplication property, this essentially breaks the contract of being a project system because we don't implement the
VBProjectProperties3interface (not that we ever did). Even the example in the code comments wouldn't work (not that it ever would for CPS).Since we own the generator and all associated code for this an alternate approach would be to just store the properties on the
MyApplicationPropertiesclass in the project file and have the generator honour them. There are only 8, they're all strings (or convertible to strings), and I assume this is not an actively developed area of the product so are unlikely to change. This would mean having separate property pages for us and legacy though, so might be not ideal, though we'd use inheritance to maximise the code reuse.